RepositoryController.java

/*
 * Copyright 2022 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.genesys.server.api.admin.v1;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.genesys.filerepository.InvalidRepositoryPathException;
import org.genesys.filerepository.NoSuchRepositoryFileException;
import org.genesys.filerepository.model.ImageGallery;
import org.genesys.filerepository.model.RepositoryFile;
import org.genesys.filerepository.service.ImageGalleryService;
import org.genesys.filerepository.service.RepositoryService;
import org.genesys.server.api.ApiBaseController;
import org.genesys.server.mvc.BaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * The Class RepositoryController.
 */
@RestController("adminRepositoryControllerV1")
@RequestMapping(RepositoryController.CONTROLLER_URL)
@PreAuthorize("hasRole('ADMINISTRATOR')")
public class RepositoryController extends BaseController {

	/** The Constant CONTROLLER_URL. */
	public static final String CONTROLLER_URL = ApiBaseController.APIv1_BASE + RepositoryController.CONTROLLER_PATH;

	/** The Constant CONTROLLER_PATH. */
	public static final String CONTROLLER_PATH = "/admin/r";

	/** The Constant LOG. */
	public static final Logger LOG = LoggerFactory.getLogger(RepositoryController.class);

	/** The repository service. */
	@Autowired
	private RepositoryService repositoryService;

	/** The image gallery service. */
	@Autowired
	private ImageGalleryService imageGalleryService;

	/**
	 * List all files.
	 *
	 * @param repositoryPath the repository path
	 * @return the list of files
	 * @throws InvalidRepositoryPathException the invalid repository path exception
	 */
	@GetMapping(value = "/files", produces = { MediaType.APPLICATION_JSON_VALUE })
	public List<RepositoryFile> listAllFiles(@RequestParam(defaultValue = "/") String repositoryPath) throws InvalidRepositoryPathException {
		Path path = Paths.get(repositoryPath);

		if (LOG.isDebugEnabled()) {
			LOG.debug("Listing files for path={}", path);
		}

		return repositoryService.getFiles(path, RepositoryFile.DEFAULT_SORT);
	}

	/**
	 * Get image gallery.
	 *
	 * @param repositoryPath the repository path
	 * @return the image gallery
	 * @throws InvalidRepositoryPathException the invalid repository path exception
	 */
	@GetMapping(value = "/image-gallery", produces = { MediaType.APPLICATION_JSON_VALUE })
	public ImageGallery getImageGallery(@RequestParam(defaultValue = "/") String repositoryPath) throws InvalidRepositoryPathException {
		Path path = Paths.get(repositoryPath);

		if (LOG.isDebugEnabled()) {
			LOG.debug("Listing files for path={}", path);
		}

		return imageGalleryService.loadImageGallery(path);
	}

	/**
	 * Gets the repository file.
	 *
	 * @param uuid the uuid
	 * @return the repository file
	 * @throws NoSuchRepositoryFileException the no such repository file exception
	 */
	@GetMapping(value = "/file/{uuid}", produces = { MediaType.APPLICATION_JSON_VALUE })
	public RepositoryFile getFile(@PathVariable UUID uuid) throws NoSuchRepositoryFileException {
		return repositoryService.getFile(uuid);
	}

	/**
	 * Upload file.
	 *
	 * @param file the file
	 * @param repositoryPath the repository path
	 *
	 */
	@PostMapping(value = "/upload")
	public void uploadFile(@RequestParam MultipartFile file, @RequestParam String repositoryPath) throws Exception {
		repositoryService.addFile(Paths.get(repositoryPath), file.getOriginalFilename(), file.getContentType(), file.getInputStream(), null);
	}

	/**
	 * Update metadata.
	 *
	 * @param fileData the file data
	 * @return the updated file
	 * @throws NoSuchRepositoryFileException the no such repository file exception
	 */
	@PostMapping(value = "/update", produces = { MediaType.APPLICATION_JSON_VALUE })
	public RepositoryFile updateMetadata(@RequestBody RepositoryFile fileData) throws NoSuchRepositoryFileException {
		return repositoryService.updateMetadata(fileData);
	}

	/**
	 * Delete file.
	 *
	 * @param uuid the uuid
	 *
	 */
	@DeleteMapping(value = "file/{uuid}")
	public void deleteFile(@PathVariable("uuid") UUID uuid) throws Exception {
		RepositoryFile repositoryFile = repositoryService.getFile(uuid);
		repositoryService.removeFile(repositoryFile);
	}

	/**
	 * Inits the binder.
	 *
	 * @param binder the binder
	 */
	@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));
	}
}