Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
minerva
core
Commits
aca38ed1
Commit
aca38ed1
authored
Jun 25, 2021
by
Piotr Gawron
Browse files
file controlloer separated from service
parent
50da71d3
Changes
4
Hide whitespace changes
Inline
Side-by-side
rest-api/src/main/java/lcsb/mapviewer/api/files/FileController.java
View file @
aca38ed1
package
lcsb.mapviewer.api.files
;
import
org.apache.commons.lang3.ArrayUtils
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.http.MediaType
;
import
org.springframework.security.access.prepost.PostAuthorize
;
...
...
@@ -21,13 +22,11 @@ import lcsb.mapviewer.services.interfaces.IUserService;
@RequestMapping
(
value
=
"/api/files"
,
produces
=
MediaType
.
APPLICATION_JSON_VALUE
)
public
class
FileController
extends
BaseController
{
private
FileRestImpl
fileRest
;
private
IUserService
userService
;
private
IFileService
fileService
;
@Autowired
public
FileController
(
FileRestImpl
fileRest
,
IUserService
userService
,
IFileService
fileService
)
{
this
.
fileRest
=
fileRest
;
public
FileController
(
IUserService
userService
,
IFileService
fileService
)
{
this
.
userService
=
userService
;
this
.
fileService
=
fileService
;
}
...
...
@@ -55,14 +54,30 @@ public class FileController extends BaseController {
@PostAuthorize
(
"hasAuthority('IS_ADMIN') or returnObject.owner == authentication.name"
)
@GetMapping
(
value
=
"/{id}"
)
public
UploadedFileEntry
getFile
(
@PathVariable
(
value
=
"id"
)
Integer
id
)
throws
ObjectNotFoundException
{
return
fileRest
.
getFile
(
id
);
UploadedFileEntry
fileEntry
=
fileService
.
getById
(
id
);
if
(
fileEntry
==
null
)
{
throw
new
ObjectNotFoundException
(
"File not found"
);
}
return
fileEntry
;
}
@PreAuthorize
(
"@fileService.getOwnerByFileId(#id)?.login == authentication.name"
)
@PostMapping
(
value
=
"/{id}:uploadContent"
)
public
UploadedFileEntry
uploadContent
(
@PathVariable
(
value
=
"id"
)
Integer
id
,
@RequestBody
byte
[]
data
)
throws
QueryException
{
return
fileRest
.
uploadContent
(
id
,
data
);
UploadedFileEntry
fileEntry
=
fileService
.
getById
(
id
);
if
(
fileEntry
==
null
)
{
throw
new
ObjectNotFoundException
(
"File not found"
);
}
long
missingByteLength
=
fileEntry
.
getLength
()
-
fileEntry
.
getFileContent
().
length
;
if
(
data
.
length
>
missingByteLength
)
{
throw
new
QueryException
(
"Too many bytes sent. There are "
+
missingByteLength
+
" missing bytes, but "
+
data
.
length
+
" sent."
);
}
byte
[]
newConent
=
ArrayUtils
.
addAll
(
fileEntry
.
getFileContent
(),
data
);
fileEntry
.
setFileContent
(
newConent
);
fileService
.
update
(
fileEntry
);
return
fileEntry
;
}
}
\ No newline at end of file
rest-api/src/main/java/lcsb/mapviewer/api/files/FileRestImpl.java
deleted
100644 → 0
View file @
50da71d3
package
lcsb.mapviewer.api.files
;
import
org.apache.commons.lang3.ArrayUtils
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
lcsb.mapviewer.api.BaseRestImpl
;
import
lcsb.mapviewer.model.cache.UploadedFileEntry
;
import
lcsb.mapviewer.persist.dao.cache.UploadedFileEntryDao
;
import
lcsb.mapviewer.services.ObjectNotFoundException
;
import
lcsb.mapviewer.services.QueryException
;
import
lcsb.mapviewer.services.interfaces.IFileService
;
@Service
public
class
FileRestImpl
extends
BaseRestImpl
{
private
UploadedFileEntryDao
uploadedFileEntryDao
;
private
IFileService
fileService
;
@Autowired
public
FileRestImpl
(
UploadedFileEntryDao
uploadedFileEntryDao
,
IFileService
fileService
)
{
this
.
uploadedFileEntryDao
=
uploadedFileEntryDao
;
this
.
fileService
=
fileService
;
}
public
UploadedFileEntry
getFile
(
Integer
id
)
throws
ObjectNotFoundException
{
UploadedFileEntry
fileEntry
=
fileService
.
getById
(
id
);
if
(
fileEntry
==
null
)
{
throw
new
ObjectNotFoundException
(
"Object not found"
);
}
return
fileEntry
;
}
public
UploadedFileEntry
uploadContent
(
Integer
id
,
byte
[]
data
)
throws
QueryException
{
int
fileId
=
Integer
.
valueOf
(
id
);
UploadedFileEntry
fileEntry
=
fileService
.
getById
(
fileId
);
if
(
fileEntry
==
null
)
{
throw
new
ObjectNotFoundException
(
"Object not found"
);
}
long
missingByteLength
=
fileEntry
.
getLength
()
-
fileEntry
.
getFileContent
().
length
;
if
(
data
.
length
>
missingByteLength
)
{
throw
new
QueryException
(
"Too many bytes sent. There are "
+
missingByteLength
+
" missing bytes, but "
+
data
.
length
+
" sent."
);
}
byte
[]
newConent
=
ArrayUtils
.
addAll
(
fileEntry
.
getFileContent
(),
data
);
fileEntry
.
setFileContent
(
newConent
);
uploadedFileEntryDao
.
update
(
fileEntry
);
return
fileEntry
;
}
public
UploadedFileEntryDao
getUploadedFileEntryDao
()
{
return
uploadedFileEntryDao
;
}
public
void
setUploadedFileEntryDao
(
UploadedFileEntryDao
uploadedFileEntryDao
)
{
this
.
uploadedFileEntryDao
=
uploadedFileEntryDao
;
}
}
service/src/main/java/lcsb/mapviewer/services/impl/FileService.java
View file @
aca38ed1
...
...
@@ -45,4 +45,9 @@ public class FileService implements IFileService {
uploadedFileEntryDao
.
add
(
entry
);
}
@Override
public
void
update
(
UploadedFileEntry
fileEntry
)
{
uploadedFileEntryDao
.
update
(
fileEntry
);
}
}
service/src/main/java/lcsb/mapviewer/services/interfaces/IFileService.java
View file @
aca38ed1
...
...
@@ -11,4 +11,6 @@ public interface IFileService {
void
add
(
UploadedFileEntry
entry
);
void
update
(
UploadedFileEntry
fileEntry
);
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment