FilesEndpoint.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.soap;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

import lombok.extern.slf4j.Slf4j;
import org.genesys.filerepository.InvalidRepositoryFileDataException;
import org.genesys.filerepository.InvalidRepositoryPathException;
import org.genesys.filerepository.NoSuchRepositoryFileException;
import org.gringlobal.compatibility.service.FilesService;
import org.gringlobal.soap.model.DeleteImage;
import org.gringlobal.soap.model.DeleteImageResponse;
import org.gringlobal.soap.model.DownloadImage;
import org.gringlobal.soap.model.DownloadImageResponse;
import org.gringlobal.soap.model.UploadImage;
import org.gringlobal.soap.model.UploadImageResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;

@Endpoint
@Slf4j
public class FilesEndpoint extends BaseEndpoint {

	@Autowired
	private FilesService filesService;

	public FilesEndpoint() {
		log.warn("Made " + getClass() + " endpoints!");
	}

	@PreAuthorize("hasAuthority('GROUP_CTUSERS') || hasAuthority('GROUP_ADMINS')")
	@PayloadRoot(namespace = GGXml.NAMESPACE_URI, localPart = "UploadImage")
	@ResponsePayload
	public UploadImageResponse upload(@RequestPayload UploadImage request) throws InvalidRepositoryPathException, InvalidRepositoryFileDataException, IOException {

		log.info("Received upload request: {}", request.getAppOrDocRelativePath());

		UploadImageResponse response = new UploadImageResponse();
		response.setUploadImageResult(filesService.upload(request.getAppOrDocRelativePath(), request.getImageBytes(), request.isCreateThumbnail(), request.isOverwriteIfExists()));
		return response;
	}

	@PayloadRoot(namespace = GGXml.NAMESPACE_URI, localPart = "DownloadImage")
	@ResponsePayload
	public DownloadImageResponse download(@RequestPayload DownloadImage request) throws NoSuchRepositoryFileException, InvalidRepositoryPathException, IOException {

		log.debug("Received download request: {}", request.getAppOrDocRelativePath());
		
		// GG Curator Tool has funny ideas about file paths: ~/uploads/images
		if (request.getAppOrDocRelativePath().startsWith("~/uploads/images")) {
			request.setAppOrDocRelativePath(request.getAppOrDocRelativePath().substring("~/uploads/images".length()));
		}

		log.info("Received download request: {}", request.getAppOrDocRelativePath());

		DownloadImageResponse response = new DownloadImageResponse();
		Path filePath = Paths.get("/", request.getAppOrDocRelativePath());

		try {
			// And thumbnails
			if (filePath.getFileName().toString().contains("_thumbnail.")) {
				response.setDownloadImageResult(filesService.thumbnail(request.getAppOrDocRelativePath()));
			} else {
				response.setDownloadImageResult(filesService.download(request.getAppOrDocRelativePath()));
			}
		} catch (Throwable e) {
			log.error(e.getMessage(), e);
		}
		return response;
	}

	@PayloadRoot(namespace = GGXml.NAMESPACE_URI, localPart = "DeleteImage")
	@ResponsePayload
	public DeleteImageResponse delete(@RequestPayload DeleteImage request) throws NoSuchRepositoryFileException, InvalidRepositoryPathException, IOException {

		log.info("Received download request: {}", request.getImageFileName());

		DeleteImageResponse response = new DeleteImageResponse();
		response.setDeleteImageResult(filesService.delete(request.getImageFileName()));
		return response;
	}

}