Commit 0bb6933b authored by Matija Obreza's avatar Matija Obreza

Licensing and file repo configuration

parent 4d70e3b7
/**
* Copyright 2016 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
package org.genesys2.server.servlet.controller;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.genesys2.server.filerepository.NoSuchRepositoryFileException;
......@@ -11,10 +33,6 @@ import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.UUID;
@Controller
public class FileRepositoryController extends BaseController {
......@@ -23,27 +41,29 @@ public class FileRepositoryController extends BaseController {
@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 IOException {
RepositoryFile repositoryFile = null;
byte[] data = null;
@RequestMapping(value = "/repository/{uuid}.{ext}", method = RequestMethod.GET)
public void downloadFile(@PathVariable String uuid, @PathVariable String ext, HttpServletResponse response) throws IOException {
try {
repositoryFile = repositoryService.getFile(UUID.fromString(uuid));
path = "/".concat(path).concat("/");
data = repositoryService.getFileBytes(path, repositoryFile.getFilename());
RepositoryFile repositoryFile = repositoryService.getFile(UUID.fromString(uuid));
byte[] data = repositoryService.getFileBytes(repositoryFile.getPath(), repositoryFile.getFilename());
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();
}
} catch (NoSuchRepositoryFileException e) {
LOG.error("File doesn't exist!", e);
throw new FileNotFoundException(e.getMessage());
}
}
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();
}
@RequestMapping(value = "/repository/{path:.+}/{uuid}.{ext}", method = RequestMethod.GET)
public void downloadFileWithPath(@PathVariable String path, @PathVariable String uuid, @PathVariable String ext, HttpServletResponse response)
throws IOException {
downloadFile(uuid, ext, response);
}
}
/**
* Copyright 2016 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
package org.genesys2.server.servlet.controller.admin;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
......@@ -14,19 +37,15 @@ 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.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.InitBinder;
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.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")
@PreAuthorize("hasRole('ADMINISTRATOR')")
......@@ -38,7 +57,7 @@ public class RepositoryController extends BaseController {
private RepositoryService repositoryService;
@RequestMapping(value = "/files", method = RequestMethod.GET)
public String listAllFiles(@RequestParam String repositoryPath, @RequestParam(required = false) String errorMessage,
public String listAllFiles(@RequestParam(defaultValue="/") String repositoryPath, @RequestParam(required = false) String errorMessage,
ModelMap model) {
List<RepositoryFile> fileList = repositoryService.getFiles(repositoryPath);
model.addAttribute("fileList", fileList);
......
/**
* Copyright 2016 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
package org.genesys2.spring.config;
import java.io.File;
import org.genesys2.server.filerepository.service.BytesStorageService;
import org.genesys2.server.filerepository.service.impl.FilesystemStorageServiceImpl;
import org.springframework.beans.factory.annotation.Value;
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;
@Configuration
public class FileRepositoryConfig {
@Value("${file.repository.dir}")
private String fileRepositoryDir;
@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();
storageService.setRepositoryBaseDirectory(repoDir);
if (fileRepositoryDir != null) {
File repoDir = new File(fileRepositoryDir);
storageService.setRepositoryBaseDirectory(repoDir);
} else {
// Using tmpdir
storageService.setRepositoryBaseDirectory(new File(System.getProperty("java.io.tmpdir")));
}
return storageService;
}
@Bean
public CommonsMultipartResolver multipartResolver() {
CommonsMultipartResolver resolver=new CommonsMultipartResolver();
CommonsMultipartResolver resolver = new CommonsMultipartResolver();
resolver.setDefaultEncoding("utf-8");
return resolver;
}
......
......@@ -155,8 +155,12 @@ transifex.content.template=<html xmlns="http://www.w3.org/1999/xhtml">\
<body>%s</body>\
</html>
#S3 parameter values
# File repository
file.repository.dir=${download.files.dir}/repository
# File repository S3 parameter values
s3.accessKey=
s3.secretKey=
s3.bucket=
s3.region=
\ No newline at end of file
s3.region=
......@@ -26,11 +26,12 @@
<%--</form>--%>
<%--</div>--%>
</div>
<ul class="funny-list">
<c:forEach var="file" items="${fileList}" varStatus="i">
<div class="row">
<li class="${status.count % 2 == 0 ? 'even' : 'odd'} 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>
<a href="<c:url value="/repository${currentPath}${file.uuid}${file.extension}" />"> <c:out value="${file.title}" /> <small>${file.originalFilename}</small></a>
</div>
<div class="col-md-2">
<a href="<c:url value="/admin/repository/edit" />?uuid=${file.uuid}&repositoryPath=${currentPath}"
......@@ -43,7 +44,8 @@
<input type="submit" value="Delete file" class="btn btn-default">
</form>
</div>
</div>
</li>
</c:forEach>
</ul>
</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