Commit 4d70e3b7 authored by Aleksandr Sharaban's avatar Aleksandr Sharaban

FileRepo: inclusion in genesys2-server.

parent 705863c5
......@@ -483,7 +483,7 @@
<dependency>
<groupId>org.genesys-pgr</groupId>
<artifactId>file-repository</artifactId>
<version>0.3-SNAPSHOT</version>
<version>0.4-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
......
package org.genesys2.server.servlet.controller;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.genesys2.server.filerepository.NoSuchRepositoryFileException;
import org.genesys2.server.filerepository.model.RepositoryFile;
import org.genesys2.server.filerepository.service.RepositoryService;
......@@ -16,20 +18,32 @@ import java.util.UUID;
@Controller
public class FileRepositoryController extends BaseController {
public static final Log LOG = LogFactory.getLog(FileRepositoryController.class);
@Autowired
private RepositoryService repositoryService;
@RequestMapping(value = "/repository/{path}/{uuid}.{ext}", method = RequestMethod.GET)
public void downloadFile(@PathVariable String path, @PathVariable String uuid,
@PathVariable String ext, HttpServletResponse response)
throws NoSuchRepositoryFileException, IOException {
RepositoryFile repositoryFile = repositoryService.getFile(UUID.fromString(uuid));
byte[] data = repositoryService.getFileBytes(path, repositoryFile.getOriginalFilename());
response.setContentType(repositoryFile.getMimeType());
response.addHeader("Content-Disposition", String.format("attachment; filename=\"%s\"",
repositoryFile.getOriginalFilename()));
response.getOutputStream().write(data);
response.flushBuffer();
throws IOException {
RepositoryFile repositoryFile = null;
byte[] data = null;
try {
repositoryFile = repositoryService.getFile(UUID.fromString(uuid));
path = "/".concat(path).concat("/");
data = repositoryService.getFileBytes(path, repositoryFile.getFilename());
} catch (NoSuchRepositoryFileException e) {
LOG.error("File doesn't exist!", e);
}
if (repositoryFile != null && data != null) {
response.setContentType(repositoryFile.getMimeType());
response.addHeader("Content-Disposition", String.format("attachment; filename=\"%s\"",
repositoryFile.getOriginalFilename()));
response.getOutputStream().write(data);
response.flushBuffer();
}
}
}
package org.genesys2.server.servlet.controller.admin;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.genesys2.server.filerepository.InvalidRepositoryFileDataException;
......@@ -9,17 +10,22 @@ import org.genesys2.server.filerepository.model.RepositoryFile;
import org.genesys2.server.filerepository.service.RepositoryService;
import org.genesys2.server.servlet.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;
@Controller
@RequestMapping("/admin/repository")
......@@ -32,28 +38,107 @@ public class RepositoryController extends BaseController {
private RepositoryService repositoryService;
@RequestMapping(value = "/files", method = RequestMethod.GET)
public String listAllFiles(@RequestParam String repositoryPath, ModelMap model) {
public String listAllFiles(@RequestParam String repositoryPath, @RequestParam(required = false) String errorMessage,
ModelMap model) {
List<RepositoryFile> fileList = repositoryService.getFiles(repositoryPath);
model.addAttribute("fileList", fileList);
model.addAttribute("currentPath", repositoryPath);
return "/admin/index";
if (StringUtils.isNotEmpty(errorMessage)) {
model.addAttribute("errorMessage", errorMessage);
}
return "/admin/filerepository/index";
}
@RequestMapping(value = "/edit", method = RequestMethod.GET)
public String getEditPage(@RequestParam String uuid, @RequestParam String repositoryPath, ModelMap model)
throws NoSuchRepositoryFileException {
RepositoryFile file = repositoryService.getFile(UUID.fromString(uuid));
model.addAttribute("file", file);
model.addAttribute("currentPath", repositoryPath);
return "/admin/filerepository/edit";
}
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public void uploadFile(@RequestParam MultipartFile file, @RequestParam String repositoryPath) throws IOException,
InvalidRepositoryPathException, InvalidRepositoryFileDataException {
public String uploadFile(@RequestParam MultipartFile file, @RequestParam String repositoryPath,
RedirectAttributes redirectAttributes) throws IOException {
String mimeType = file.getContentType();
if (mimeType.startsWith("image")) {
repositoryService.addImage(repositoryPath, file.getOriginalFilename(), file.getContentType(),
file.getBytes(), null);
try {
if (mimeType.startsWith("image")) {
repositoryService.addImage(repositoryPath, file.getOriginalFilename(), file.getContentType(),
file.getBytes(), null);
} else {
repositoryService.addFile(repositoryPath, file.getOriginalFilename(), file.getContentType(),
file.getBytes(), null);
}
} catch (InvalidRepositoryPathException e) {
LOG.error("Invalid repository path!", e);
redirectAttributes.addAttribute("errorMessage", "Invalid repository path!");
} catch (InvalidRepositoryFileDataException e) {
LOG.error("Invalid file data!", e);
redirectAttributes.addAttribute("errorMessage", "Invalid file data!");
}
repositoryService.addFile(repositoryPath, file.getOriginalFilename(), file.getContentType(),
file.getBytes(), null);
redirectAttributes.addAttribute("repositoryPath", repositoryPath);
return "redirect:/admin/repository/files";
}
@RequestMapping(value = "/update", method = RequestMethod.POST)
public void updateMetadata(@ModelAttribute RepositoryFile repositoryFile) throws NoSuchRepositoryFileException {
repositoryService.updateFile(repositoryFile);
public String updateMetadata(@ModelAttribute RepositoryFile repositoryFile, @RequestParam String uuid,
@RequestParam String repositoryPath, RedirectAttributes redirectAttributes)
throws NoSuchRepositoryFileException {
RepositoryFile updatedFile = repositoryService.getFile(UUID.fromString(uuid));
try {
updateMetadata(repositoryFile, updatedFile);
} catch (ParseException e) {
LOG.error("Incorrect date format. Should be 'yyyy-MM-dd HH:mm:ss'.", e);
redirectAttributes.addAttribute("errorMessage", "Incorrect date format. Should be 'yyyy-MM-dd HH:mm:ss'.");
redirectAttributes.addAttribute("uuid", updatedFile.getUuid().toString());
return "redirect:/admin/repository/edit";
}
repositoryService.updateFile(updatedFile);
redirectAttributes.addAttribute("repositoryPath", repositoryPath);
return "redirect:/admin/repository/files";
}
@RequestMapping(value = "/delete", method = RequestMethod.POST)
public String deleteFile(@RequestParam String uuid, @RequestParam String repositoryPath,
RedirectAttributes redirectAttributes) throws NoSuchRepositoryFileException, IOException {
RepositoryFile repositoryFile = repositoryService.getFile(UUID.fromString(uuid));
repositoryService.removeFile(repositoryFile);
redirectAttributes.addAttribute("repositoryPath", repositoryPath);
return "redirect:/admin/repository/files";
}
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
}
private void updateMetadata(RepositoryFile originalFile, RepositoryFile updatedFile) throws ParseException {
updatedFile.setTitle(originalFile.getTitle());
updatedFile.setSubject(originalFile.getSubject());
updatedFile.setDescription(originalFile.getDescription());
updatedFile.setCreator(originalFile.getCreator());
updatedFile.setCreated(originalFile.getCreated());
updatedFile.setRightsHolder(originalFile.getRightsHolder());
updatedFile.setAccessRights(originalFile.getAccessRights());
updatedFile.setLicense(originalFile.getLicense());
updatedFile.setMimeType(originalFile.getMimeType());
updatedFile.setExtent(originalFile.getExtent());
updatedFile.setBibliographicCitation(originalFile.getBibliographicCitation());
updatedFile.setCreatedDate(originalFile.getCreatedDate());
updatedFile.setLastModifiedDate(originalFile.getLastModifiedDate());
}
}
......@@ -28,7 +28,7 @@ import org.springframework.context.annotation.ImportResource;
@Configuration
@Import({ SpringProperties.class, SpringCommonConfig.class, SpringAclConfig.class, SpringSchedulerConfig.class, SpringDataBaseConfig.class,
SpringMailConfig.class, SpringSecurityOauthConfig.class, SpringCacheConfig.class, ElasticsearchConfig.class })
SpringMailConfig.class, SpringSecurityOauthConfig.class, SpringCacheConfig.class, ElasticsearchConfig.class, FileRepositoryConfig.class })
@ImportResource({ "classpath:/spring/spring-security.xml" })
public class ApplicationConfig {
public static final Log LOG = LogFactory.getLog(ApplicationConfig.class);
......
package org.genesys2.spring.config;
import org.genesys2.server.filerepository.service.BytesStorageService;
import org.genesys2.server.filerepository.service.RepositoryService;
import org.genesys2.server.filerepository.service.impl.FilesystemStorageServiceImpl;
import org.genesys2.server.filerepository.service.impl.RepositoryServiceImpl;
import org.genesys2.server.filerepository.service.impl.S3StorageServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import java.io.File;
......@@ -14,6 +13,7 @@ import java.io.File;
public class FileRepositoryConfig {
@Bean(name = "fileSystemStorage")
@Primary
public BytesStorageService bytesStorageService() {
File repoDir = new File(System.getProperty("java.io.tmpdir")); //FIXME Set root directory for the service
FilesystemStorageServiceImpl storageService = new FilesystemStorageServiceImpl();
......@@ -22,13 +22,10 @@ public class FileRepositoryConfig {
return storageService;
}
@Bean(name = "s3Storage")
public BytesStorageService S3StorageService() {
return new S3StorageServiceImpl();
}
@Bean
public RepositoryService fileRepositoryService() {
return new RepositoryServiceImpl();
public CommonsMultipartResolver multipartResolver() {
CommonsMultipartResolver resolver=new CommonsMultipartResolver();
resolver.setDefaultEncoding("utf-8");
return resolver;
}
}
<!DOCTYPE html>
<%@include file="/WEB-INF/jsp/init.jsp" %>
<html>
<head>
<title><spring:message code="admin.page.title"/></title>
</head>
<body>
<div class="row">
<div class="col-md-offset-1 col-md-4">
<a href="<c:url value="/admin/repository/files" />?repositoryPath=${currentPath}" class="btn btn-default">Back</a>
<h4>Updating metadata for file '${file.originalFilename}'</h4><br/>
<form action="<c:out value="/admin/repository/update" />?${_csrf.parameterName}=${_csrf.token}" method="post">
<input type="hidden" name="uuid" value="${file.uuid}">
<input type="hidden" name="repositoryPath" value="${currentPath}">
<div class="form-group">
<label for="title">Title</label>
<input type="text" id="title" name="title" value="${file.title}" class="form-control">
</div>
<div class="form-group">
<label for="subject">Subject</label>
<input type="text" id="subject" name="subject" value="${file.subject}" class="form-control">
</div>
<div class="form-group">
<label for="description">Description</label>
<input type="text" id="description" name="description" value="${file.description}" class="form-control">
</div>
<div class="form-group">
<label for="creator">Creator</label>
<input type="text" id="creator" name="creator" value="${file.creator}" class="form-control">
</div>
<div class="form-group">
<label for="created">Created</label>
<input type="text" id="created" name="created" value="${file.created}" class="form-control">
</div>
<div class="form-group">
<label for="rightsHolder">Rights Holder</label>
<input type="text" id="rightsHolder" name="rightsHolder" value="${file.rightsHolder}" class="form-control">
</div>
<div class="form-group">
<label for="accessRights">Access Rights</label>
<input type="text" id="accessRights" name="accessRights" value="${file.accessRights}" class="form-control">
</div>
<div class="form-group">
<label for="license">License</label>
<input type="text" id="license" name="license" value="${file.license}" class="form-control">
</div>
<div class="form-group">
<label for="format">Format</label>
<input type="text" id="format" name="mimeType" value="${file.format}" class="form-control">
</div>
<div class="form-group">
<label for="extent">Extent</label>
<input type="text" id="extent" name="extent" value="${file.extent}" class="form-control">
</div>
<div class="form-group">
<label for="bibliographicCitation">Bibliographic Citation</label>
<input type="text" id="bibliographicCitation" name="bibliographicCitation"
value="${file.bibliographicCitation}" class="form-control">
</div>
<div class="form-group">
<label for="dateSubmitted">Date Submitted</label>
<input type="text" id="dateSubmitted" name="createdDate" value="${file.dateSubmitted}"
class="form-control">
</div>
<div class="form-group">
<label for="modified">Modified</label>
<input type="text" id="modified" name="lastModifiedDate" value="${file.modified}" class="form-control">
</div>
<button type="submit" class="btn btn-default">Save</button>
</form>
</div>
</div>
</body>
</html>
<!DOCTYPE html>
<%@include file="/WEB-INF/jsp/init.jsp"%>
<html>
<head>
<title><spring:message code="admin.page.title" /></title>
</head>
<body>
<c:if test="${not empty errorMessage}">
<div class="alert alert-warning">${errorMessage}</div>
</c:if>
<div class="row">
<div class="col-md-2">
<form action="<c:out value="/admin/repository/upload?${_csrf.parameterName}=${_csrf.token}" />"
method="post" enctype="multipart/form-data">
<input type="hidden" name="repositoryPath" value="${currentPath}">
<input type="file" name="file">
<button type="submit" class="btn btn-default">Upload file or image</button>
</form>
</div>
<%--<div class="col-md-1">--%>
<%--<form action="<c:out value="/admin/repository/upload" />" method="post">--%>
<%--<input type="file" name="image">--%>
<%--<button type="submit">Upload image</button>--%>
<%--</form>--%>
<%--</div>--%>
</div>
<c:forEach var="file" items="${fileList}" varStatus="i">
<div class="row">
<div class="col-md-4">
<c:out value="${i.count}."/>
<a href="<c:url value="/repository${currentPath}${file.uuid}${file.extension}" />">${file.originalFilename}</a>
</div>
<div class="col-md-2">
<a href="<c:url value="/admin/repository/edit" />?uuid=${file.uuid}&repositoryPath=${currentPath}"
class="btn btn-default">Edit metadata</a>
</div>
<div class="col-md-2">
<form action="<c:out value="/admin/repository/delete?${_csrf.parameterName}=${_csrf.token}"/>" method="post">
<input type="hidden" name="uuid" value="${file.uuid}">
<input type="hidden" name="repositoryPath" value="${currentPath}">
<input type="submit" value="Delete file" class="btn btn-default">
</form>
</div>
</div>
</c:forEach>
</body>
</html>
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