Commit 9878b545 authored by Matija Obreza's avatar Matija Obreza
Browse files

FIX: Reinstated RepositoryController for file downloads

parent 9584bc0e
/*
* Copyright 2018 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.api.v0;
import java.io.IOException;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;
import org.genesys.catalog.exceptions.InvalidApiUsageException;
import org.genesys.filerepository.NoSuchRepositoryFileException;
import org.genesys.filerepository.model.RepositoryFile;
import org.genesys.filerepository.service.RepositoryService;
import org.genesys2.server.api.ApiBaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
/**
* The Class RepositoryController.
*
* @author Matija Obreza
*/
@RestController("repositoryApi0")
@RequestMapping(RepositoryController.CONTROLLER_URL)
@PreAuthorize("isAuthenticated()")
@Api(tags = { "repository" })
public class RepositoryController {
/** The Constant API_BASE. */
protected static final String CONTROLLER_URL = ApiBaseController.APIv0_BASE + "/repository";
/** The repository service. */
@Autowired
protected RepositoryService repositoryService;
/**
* Gets the file.
*
* @param fileUuid the file uuid
* @return the file
* @throws NoSuchRepositoryFileException the no such repository file exception
* @throws IOException Signals that an I/O exception has occurred.
*/
@GetMapping(value = "/{fileUuid}")
public RepositoryFile getFile(@PathVariable("fileUuid") final UUID fileUuid) throws NoSuchRepositoryFileException, IOException {
return repositoryService.getFile(fileUuid);
}
/**
* Update file.
*
* @param fileUuid the file uuid
* @param version the version
* @param metadata the metadata
* @return the repository file
* @throws NoSuchRepositoryFileException the no such repository file exception
* @throws IOException Signals that an I/O exception has occurred.
*/
@PostMapping(value = "/{fileUuid},{version}")
public RepositoryFile updateFile(@PathVariable("fileUuid") final UUID fileUuid, @PathVariable("version") final int version, final RepositoryFile metadata)
throws NoSuchRepositoryFileException, IOException {
if (metadata != null && metadata.getUuid().equals(fileUuid) && metadata.getVersion().equals(version)) {
return repositoryService.updateMetadata(repositoryService.getFile(fileUuid, version));
} else {
throw new InvalidApiUsageException("File uuid and version don't match");
}
}
/**
* Removes the file.
*
* @param fileUuid the file uuid
* @return the repository file
* @throws NoSuchRepositoryFileException the no such repository file exception
* @throws IOException Signals that an I/O exception has occurred.
*/
@DeleteMapping(value = "/{fileUuid}")
public RepositoryFile removeFile(@PathVariable("fileUuid") final UUID fileUuid) throws NoSuchRepositoryFileException, IOException {
return repositoryService.removeFile(repositoryService.getFile(fileUuid));
}
/**
* Download file.
*
* @param fileUuid the file uuid
* @param response the response
* @throws NoSuchRepositoryFileException the no such repository file exception
* @throws IOException Signals that an I/O exception has occurred.
*/
@GetMapping(value = "/download/{fileUuid}")
public void downloadFile(@PathVariable("fileUuid") final UUID fileUuid, final HttpServletResponse response) throws NoSuchRepositoryFileException, IOException {
final RepositoryFile repositoryFile = repositoryService.getFile(fileUuid);
response.setHeader(HttpHeaders.CACHE_CONTROL, "max-age=86400, s-maxage=86400, public, no-transform");
response.setHeader(HttpHeaders.PRAGMA, "");
response.setDateHeader(HttpHeaders.LAST_MODIFIED, repositoryFile.getLastModifiedDate().getTime());
response.setContentType(repositoryFile.getContentType());
response.addHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", repositoryFile.getOriginalFilename()));
final byte[] data = repositoryService.getFileBytes(repositoryFile);
if (data != null) {
response.setContentLength(data.length);
response.getOutputStream().write(data);
}
response.flushBuffer();
}
}
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