RepositoryController.java
/*
* Copyright 2019 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.gringlobal.api.v2.impl;
import com.fasterxml.jackson.annotation.JsonView;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.genesys.blocks.model.JsonViews;
import org.genesys.filerepository.FolderNotEmptyException;
import org.genesys.filerepository.InvalidRepositoryFileDataException;
import org.genesys.filerepository.InvalidRepositoryPathException;
import org.genesys.filerepository.NoSuchRepositoryFileException;
import org.gringlobal.api.ApiBaseController;
import org.gringlobal.api.Pagination;
import org.gringlobal.api.exception.NotFoundElement;
import org.gringlobal.api.model.ImageGalleryDTO;
import org.gringlobal.api.model.RepositoryFileDTO;
import org.gringlobal.api.model.RepositoryFolderDTO;
import org.gringlobal.api.v2.facade.RepositoryApiService;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.MediaType;
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.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.HandlerMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
@RestController("repositoryApi2")
@RequestMapping(RepositoryController.CONTROLLER_URL)
@PreAuthorize("isAuthenticated()")
@Tag(name = "Repository")
@Slf4j
public class RepositoryController extends ApiBaseController {
/** The Constant CONTROLLER_URL. */
public static final String CONTROLLER_URL = ApiBaseController.APIv2_BASE + "/repository";
/** The repository service. */
@Autowired
protected RepositoryApiService repositoryApiService;
/**
* Gets the file.
*
* @param fileUuid the file uuid
* @return the file
* @throws NoSuchRepositoryFileException the no such repository file exception
*/
@GetMapping(value = "/file/{fileUuid}")
public RepositoryFileDTO getFile(@PathVariable("fileUuid") final UUID fileUuid) throws NoSuchRepositoryFileException {
return repositoryApiService.getFile(fileUuid);
}
// /**
// * Download file metadata of specified folder.
// *
// * @param request the request
// * @param response the response
// * @throws NotFoundElement the no such repository folder
// * @throws IOException Signals that an I/O exception has occurred.
// * @throws InvalidRepositoryPathException the invalid repository path exception
// */
// @GetMapping(value = "/download/folder-metadata/**")
// public void downloadFolderMetadata(final HttpServletRequest request, final HttpServletResponse response) throws NotFoundElement, IOException, InvalidRepositoryPathException {
// final String folderPath = ((String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE)).substring((CONTROLLER_URL + "/download/folder-metadata").length());
// final RepositoryFolder folder = repositoryService.getFolder(Paths.get(folderPath));
// if (folder == null) {
// throw new NotFoundElement("No folder with path=" + folderPath);
// }
// response.setContentType("text/csv;charset=UTF-8");
// response.setHeader("Content-Disposition", "attachment; filename=" + folder.getName() + "_files_metadata.csv ");
//
// Stream<RepositoryFile> files = repositoryService.streamFiles(Paths.get(folder.getPath()), RepositoryFile.DEFAULT_SORT);
// filesMetadataInfo.downloadMetadata(files, response, '\t', '"', '\\', "\n", "UTF-8");
// }
/**
* 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.
*/
@RequestMapping(value = "/download/{fileUuid:\\w{8}\\-\\w{4}.+}", method = { RequestMethod.GET, RequestMethod.POST })
public void downloadFile(@PathVariable("fileUuid") final UUID fileUuid, final HttpServletRequest request, final HttpServletResponse response) throws NoSuchRepositoryFileException, IOException {
repositoryApiService.downloadFile(fileUuid, request, response);
}
/**
* Download folder as zip.
*
* @param folderUuid the folder uuid
* @throws IOException error in reading from repository or writing to zip
* @throws InvalidRepositoryPathException the invalid repository path of folder
*/
@RequestMapping(value = "/folder/download/{folderUuid:\\w{8}\\-\\w{4}.+}", method = { RequestMethod.GET, RequestMethod.POST })
public void downloadFolderAsZip(@PathVariable("folderUuid") final UUID folderUuid, final HttpServletResponse response) throws IOException, InvalidRepositoryPathException {
repositoryApiService.downloadFolderAsZip(folderUuid, response);
}
/**
* Extract zip in repository folder.
*
* @param fileUuid the repository zip file uuid
* @return the list of created repository files from zip
* @throws IOException error in writing to repository
* @throws NoSuchRepositoryFileException the repository file doesn't exist
* @throws InvalidRepositoryPathException error in creating sub repository folders
* @throws InvalidRepositoryFileDataException if the target repositoryFile doesn't have a zip extension.
*/
@GetMapping(value = "/file/extract/{fileUuid:\\w{8}\\-\\w{4}.+}")
public List<RepositoryFileDTO> extractZip(@PathVariable("fileUuid") final UUID fileUuid)
throws NoSuchRepositoryFileException, IOException, InvalidRepositoryPathException, InvalidRepositoryFileDataException {
return repositoryApiService.extractZip(fileUuid);
}
/**
* Move specified file to the specified full file path (folder + new
* originalFilename).
*
* @param fileUuid file UUID
* @param fullPath full folder path + new orignalFilename
* @return the repository file
* @throws InvalidRepositoryPathException the invalid repository path exception
* @throws InvalidRepositoryFileDataException the invalid repository file data
* exception
* @throws NoSuchRepositoryFileException the no such repository file exception
*/
@PostMapping(value = "/file/{fileUuid}/move", produces = { MediaType.APPLICATION_JSON_VALUE })
public RepositoryFileDTO moveAndRenameFile(@PathVariable("fileUuid") final UUID fileUuid, @RequestBody final String fullPath)
throws NoSuchRepositoryFileException, InvalidRepositoryPathException, InvalidRepositoryFileDataException {
return repositoryApiService.moveAndRenameFile(repositoryApiService.getFile(fileUuid), Paths.get(fullPath));
}
/**
* Rename folder.
*
* @param folderUuid the folder uuid
* @param fullPath the full path
* @return the folder details
* @throws InvalidRepositoryPathException the invalid repository path exception
*/
@PostMapping(value = "/folder/{folderUuid}/rename", produces = { MediaType.APPLICATION_JSON_VALUE })
@JsonView(JsonViews.Protected.class)
public RepositoryApiService.FolderDetails renameFolder(@PathVariable("folderUuid") final UUID folderUuid, @RequestBody final String fullPath) throws InvalidRepositoryPathException {
return repositoryApiService.renameFolder(folderUuid, fullPath);
}
/**
* Gets folder details at specified path.
*
* @param request the request
* @return the folder
* @throws InvalidRepositoryPathException the invalid repository path exception
*/
@GetMapping("/folder/**")
@Operation(operationId = "getFolder", summary = "Get folder details by folder path")
@JsonView(JsonViews.Protected.class)
public RepositoryApiService.FolderDetails getFolder(final HttpServletRequest request) throws InvalidRepositoryPathException {
final String folderPath = ((String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE)).substring((CONTROLLER_URL + "/folder").length());
return repositoryApiService.folderDetails(Paths.get(folderPath));
}
/**
* Gets the folder subfolders.
*
* @param request the request
* @param page the page
* @return the folder sub-folders
* @throws InvalidRepositoryPathException the invalid repository path exception
*/
@GetMapping(value = "/folder/**", params = { "folders" })
@Operation(operationId = "getSubfolders", summary = "List sub-folders of folder path")
@JsonView(JsonViews.Protected.class)
public Page<RepositoryFolderDTO> getFolderSubfolders(final HttpServletRequest request, @ParameterObject final Pagination page) throws InvalidRepositoryPathException {
final String folderPath = ((String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE)).substring((CONTROLLER_URL + "/folder").length());
return repositoryApiService.listFolders(Paths.get(folderPath), page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE));
}
/**
* Gets the folder files.
*
* @param request the request
* @param page the page
* @return the folder files
* @throws InvalidRepositoryPathException the invalid repository path exception
*/
@GetMapping(value = "/folder/**", params = { "files" })
@Operation(operationId = "getSubfolders", summary = "List sub-folders of folder path")
@JsonView(JsonViews.Protected.class)
public Page<RepositoryFileDTO> getFolderFiles(final HttpServletRequest request, @ParameterObject final Pagination page) throws InvalidRepositoryPathException {
final String folderPath = ((String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE)).substring((CONTROLLER_URL + "/folder").length());
return repositoryApiService.listFiles(Paths.get(folderPath), page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE));
}
/**
* Get image gallery.
*
* @param request the request
* @return the gallery
* @throws InvalidRepositoryPathException the invalid repository path exception
*/
@GetMapping("/gallery/**")
@JsonView(JsonViews.Root.class)
public ImageGalleryDTO getGallery(final HttpServletRequest request) throws InvalidRepositoryPathException {
final String folderPath = ((String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE)).substring((CONTROLLER_URL + "/gallery").length());
return repositoryApiService.getGallery(Paths.get(folderPath));
}
/**
* Create or load folder at specified path
*
* @param request the request
* @return the repository folder
*/
@PutMapping("/folder/**")
@Operation(operationId = "ensureFolder", summary = "Create or load folder at specified path")
@JsonView(JsonViews.Protected.class)
public RepositoryFolderDTO ensureFolder(final HttpServletRequest request) throws InvalidRepositoryPathException {
final String folderPath = ((String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE)).substring((CONTROLLER_URL + "/folder").length());
return repositoryApiService.ensureFolder(Paths.get(folderPath));
}
/**
* Update folder title and description
*
* @param folders the list of folders
* @return list of operation responses
*/
@PutMapping("/folder")
@Operation(operationId = "updateFolder", summary = "Update folder title and description")
@JsonView(JsonViews.Protected.class)
public List<OpResponse<RepositoryApiService.FolderDetails>> updateFolders(@RequestBody final List<RepositoryFolderDTO> folders) {
return folders.stream().map(folder -> {
try {
folder = repositoryApiService.updateFolder(folder);
return new OpResponse<>(repositoryApiService.folderDetails(folder.getFolderPath()));
} catch (Throwable e) {
return new OpResponse<RepositoryApiService.FolderDetails>(e.getCause(), folder);
}
}).collect(Collectors.toList());
}
/**
* Remove folder by specified path.
*
* @param request the request
* @return the deleted folder
* @throws InvalidRepositoryPathException the invalid repository path exception
* @throws FolderNotEmptyException
*/
@DeleteMapping("/folder/**")
@Operation(operationId = "deleteFolder", summary = "Delete folder")
@JsonView(JsonViews.Protected.class)
public RepositoryFolderDTO deleteFolder(final HttpServletRequest request)
throws InvalidRepositoryPathException, FolderNotEmptyException {
final String folderPath = ((String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE)).substring((CONTROLLER_URL + "/folder").length());
Path path = Paths.get(folderPath);
RepositoryFolderDTO folder = repositoryApiService.getFolder(path);
repositoryApiService.deleteFolder(path);
return folder;
}
/**
* Remove folders by UUIDs.
*
* @param uuids the list of files uuids
* @return list of operation responses
*/
@PostMapping(value = "/folder/remove")
@JsonView(JsonViews.Protected.class)
public List<OpResponse<RepositoryFolderDTO>> deleteFolders(@RequestBody final List<UUID> uuids) {
return uuids.stream().map(folderUUID -> {
try {
RepositoryFolderDTO folder = repositoryApiService.getFolder(folderUUID);
return new OpResponse<>(repositoryApiService.deleteFolder(folder.getFolderPath()));
} catch (Throwable e) {
return new OpResponse<RepositoryFolderDTO>(e, folderUUID);
}
}).collect(Collectors.toList());
}
/**
* Upload file to specified folder.
*
* @param file the file
* @param request the request
* @return repository file metadata
* @throws InvalidRepositoryPathException the invalid repository path exception
* @throws InvalidRepositoryFileDataException the invalid repository file data
* exception
* @throws IOException Signals that an I/O exception has occurred.
* @throws NotFoundElement the not found element
*/
@PostMapping(value = "/upload/**")
public RepositoryFileDTO uploadFile(@RequestPart(name = "file", required = true) final MultipartFile file,
@RequestPart(name = "metadata", required = false) final RepositoryFileDTO metadata, final HttpServletRequest request)
throws IOException, InvalidRepositoryPathException, InvalidRepositoryFileDataException {
final String folderPath = ((String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE)).substring((CONTROLLER_URL + "/upload").length());
Path repositoryPath = Paths.get(folderPath);
log.info("Upload file {} to path {}", file.getOriginalFilename(), repositoryPath);
return repositoryApiService.addFile(repositoryPath, file.getOriginalFilename(), file.getContentType(), file.getInputStream(), metadata);
}
/**
* Update files.
*
* @param metadataList the metadata
* @return list of operation responses
*/
@PutMapping(value = "/file")
public List<OpResponse<RepositoryFileDTO>> updateFiles(@RequestBody final List<RepositoryFileDTO> metadataList) {
return metadataList.stream().map(metadata -> {
try {
return new OpResponse<>(repositoryApiService.updateMetadata(metadata));
} catch (Throwable e) {
return new OpResponse<RepositoryFileDTO>(e, metadata);
}
}).collect(Collectors.toList());
}
/**
* Removes files.
*
* @param uuids the list of files uuids
* @return list of operation responses
*/
@PostMapping(value = "/file/remove")
public List<OpResponse<RepositoryFileDTO>> removeFile(@RequestBody final List<UUID> uuids) {
return uuids.stream().map(fileUuid -> {
try {
RepositoryFileDTO removedFile = repositoryApiService.removeFile(fileUuid);
return new OpResponse<>(removedFile);
} catch (Throwable e) {
return new OpResponse<RepositoryFileDTO>(e, fileUuid);
}
}).collect(Collectors.toList());
}
/**
* Creates the gallery.
*
* @param request the request
* @return the image gallery
* @throws InvalidRepositoryPathException the invalid repository path exception
*/
@PostMapping("/gallery/**")
@JsonView(JsonViews.Root.class)
public ImageGalleryDTO createGallery(final HttpServletRequest request, @RequestBody ImageGalleryDTO metadata) throws InvalidRepositoryPathException {
final String folderPath = ((String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE)).substring((CONTROLLER_URL + "/gallery").length());
return repositoryApiService.createImageGallery(Paths.get(folderPath), metadata.getTitle(), metadata.getDescription());
}
/**
* Creates the gallery.
*
* @param request the request
* @return the image gallery
* @throws InvalidRepositoryPathException the invalid repository path exception
*/
@DeleteMapping("/gallery/**")
@JsonView(JsonViews.Root.class)
public ImageGalleryDTO removeGallery(final HttpServletRequest request) throws InvalidRepositoryPathException {
final String folderPath = ((String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE)).substring((CONTROLLER_URL + "/gallery").length());
return repositoryApiService.removeGallery(Path.of(folderPath));
}
// /**
// * Upload folder metadata file.
// *
// * @param file the file
// * @param request the request
// * @return repository files by specified folder path
// * @throws IOException Signals that an I/O exception has occurred.
// * @throws InvalidRepositoryPathException the invalid repository path exception
// */
// @PostMapping(value = "/upload/folder-metadata/**")
// public Page<RepositoryFile> uploadFolderMetadata(@RequestPart(name = "file", required = true) final MultipartFile file,
// final HttpServletRequest request) throws InvalidRepositoryPathException, IOException {
//
// final String folderPath = ((String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE)).substring((CONTROLLER_URL + "/upload/folder-metadata").length());
// filesMetadataUpdate.updateFromCsv(file.getInputStream(), '\t', '"', '\\');
// return repositoryService.listFiles(Paths.get(folderPath), Pagination.toPageRequest(50, RepositoryFile.DEFAULT_SORT));
// }
}