Commit aca38ed1 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

file controlloer separated from service

parent 50da71d3
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
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;
}
}
......@@ -45,4 +45,9 @@ public class FileService implements IFileService {
uploadedFileEntryDao.add(entry);
}
@Override
public void update(UploadedFileEntry fileEntry) {
uploadedFileEntryDao.update(fileEntry);
}
}
......@@ -11,4 +11,6 @@ public interface IFileService {
void add(UploadedFileEntry entry);
void update(UploadedFileEntry fileEntry);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment