use of org.survey.model.file.File in project survey by markoniemi.
the class FileServiceImpl method downloadFile.
@Override
public // @Produces("application/pdf")
Response downloadFile(Long id) {
File file = fileRepository.findOne(id);
// java.io.File file = new java.io.File("test.pdf");
ResponseBuilder response = Response.ok((Object) file, file.getMimeType());
response.header("Content-Disposition", "attachment; filename=" + file.getFilename());
return response.build();
}
use of org.survey.model.file.File in project survey by markoniemi.
the class FileServiceImpl method uploadFile.
@Override
public // HttpServletRequest request) {
void uploadFile(List<Attachment> attachments) {
for (Attachment attachment : attachments) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
MultivaluedMap<String, String> map = attachment.getHeaders();
String fileName = getFileName(map);
log.debug("uploadFile: {}", fileName);
if (StringUtils.isNotEmpty(fileName)) {
DataHandler handler = attachment.getDataHandler();
InputStream inputStream = handler.getInputStream();
IOUtils.copy(inputStream, outputStream);
File file = createFile(fileName, "mimeType", "owner", outputStream.toByteArray());
// TODO move to finally block
inputStream.close();
outputStream.flush();
outputStream.close();
// TODO for some reason there is an extra file with 0 size
if (file.getContent().length > 0) {
fileRepository.save(file);
}
}
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
// return Response.ok("file uploaded").build();
}
use of org.survey.model.file.File in project survey by markoniemi.
the class FileServiceImpl method createFile.
private File createFile(String filename, String mimeType, String owner, byte[] fileContent) {
File file = new File();
file.setFilename(filename);
file.setMimeType(mimeType);
file.setContent(fileContent);
// file.setOwner(owner);
file.setCreateTime(System.currentTimeMillis());
// TODO change files rest to files/:user/:filename
file.setUrl("/survey-web/api/rest/files/");
return file;
}
use of org.survey.model.file.File in project survey by markoniemi.
the class FileServiceTestBase method update.
@Test
public void update() {
create();
for (int i = 0; i < ENTITY_COUNT; i++) {
File foundEntity = entityService.findOne(savedEntities.get(i).getId());
File updatedEntity = entityFactory.getUpdatedEntity(foundEntity);
updatedEntity.setId(foundEntity.getId());
entityService.update(updatedEntity);
foundEntity = entityService.findOne(savedEntities.get(i).getId());
assertEntity(updatedEntity, foundEntity);
}
}
use of org.survey.model.file.File in project survey by markoniemi.
the class FileRepositoryTest method findAllByOwner.
@Test
public void findAllByOwner() {
save();
File originalFile = orginalEntities.get(0);
@SuppressWarnings("unchecked") List<File> files = IteratorUtils.toList(fileRepository.findAllByOwner(originalFile.getOwner()).iterator());
Assert.assertEquals(ENTITY_COUNT, files.size());
Assert.assertTrue(files.containsAll(orginalEntities));
int index = files.indexOf(originalFile);
Assert.assertEquals(0, entityComparator.compare(originalFile, files.get(index)));
}
Aggregations