Untitled

 avatar
vas
plain_text
2 months ago
8.2 kB
3
Indexable
package com.convatec.filemgmt.service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map.Entry;

import com.convatec.filemgmt.util.Utils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.GetObjectRequest;
import com.amazonaws.services.s3.model.ListObjectsRequest;
import com.amazonaws.services.s3.model.ListObjectsV2Result;
import com.amazonaws.services.s3.model.ObjectListing;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.s3.model.S3ObjectInputStream;
import com.amazonaws.services.s3.model.S3ObjectSummary;
import com.convatec.filemgmt.FileMgmtApplication;
import com.convatec.filemgmt.exceptions.FileDownloadException;
import com.convatec.filemgmt.exceptions.FileUploadException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;

@Service
public class FileMgmtService implements IFileMgmtService {

	private final AwsS3ClientBuilderService awsS3ClientBuilderService;

	@Autowired
	public FileMgmtService(AwsS3ClientBuilderService awsS3ClientBuilderService) {
		this.awsS3ClientBuilderService = awsS3ClientBuilderService;
	}

	@Override
	public String uploadFile(String bucket, String Objectkey, MultipartFile multipartFile)
			throws FileUploadException, IOException {

		JsonNode keys = getSecrets(bucket);
		String bucketId = keys.get("bucket").asText();

		final AmazonS3 s3Client = awsS3ClientBuilderService.createAwsS3Client(keys.get("api_key").asText(),
				keys.get("secret_key").asText(), Regions.US_EAST_1);
		String multipartFileName = Utils.sanitizeFilename(multipartFile.getOriginalFilename());
		// convert multipart file to a file
		File file = new File(multipartFileName);
		try (FileOutputStream fileOutputStream = new FileOutputStream(file)) {
			fileOutputStream.write(multipartFile.getBytes());
		}
		// generate file name
		String fileName = generateFileName(multipartFile);
		// upload file
		PutObjectRequest request = new PutObjectRequest(bucketId, Objectkey, file);
		ObjectMetadata metadata = new ObjectMetadata();
		metadata.setContentType("plain/" + FilenameUtils.getExtension(multipartFileName));
		metadata.addUserMetadata("Title", "File Upload - " + fileName);
		metadata.setContentLength(file.length());
		request.setMetadata(metadata);
		s3Client.putObject(request);
		URL s3Url = s3Client.getUrl(bucketId, Objectkey);
		this.awsS3ClientBuilderService.shutdown(s3Client);
		file.delete();
		return s3Url.toExternalForm();
	}

	@Override
	public byte[] downloadFile(String bucket, String fileName) throws FileDownloadException, IOException {

		JsonNode keys = getSecrets(bucket);
		String bucketId = keys.get("bucket").asText();

		final AmazonS3 s3Client = awsS3ClientBuilderService.createAwsS3Client(keys.get("api_key").asText(),
				keys.get("secret_key").asText(), Regions.US_EAST_1);

		if (bucketIsEmpty(s3Client, bucketId)) {
			this.awsS3ClientBuilderService.shutdown(s3Client);
			throw new FileDownloadException("Requested bucket does not exist or is empty");
		}
		GetObjectRequest getObjectRequest = new GetObjectRequest(bucketId, fileName);

		S3Object s3Object = s3Client.getObject(getObjectRequest);

		S3ObjectInputStream objectInputStream = s3Object.getObjectContent();

		byte[] bytes = IOUtils.toByteArray(objectInputStream);
		this.awsS3ClientBuilderService.shutdown(s3Client);
		if (bytes != null && bytes.length != 0) {
			return bytes;
		} else {
			throw new FileDownloadException("Could not find the file!");
		}
	}

	@Override
	public boolean delete(String bucket, String fileName) throws FileDownloadException {
		JsonNode keys = getSecrets(bucket);
		String bucketId = keys.get("bucket").asText();

		final AmazonS3 s3Client = awsS3ClientBuilderService.createAwsS3Client(keys.get("api_key").asText(),
				keys.get("secret_key").asText(), Regions.US_EAST_1);

		if (bucketIsEmpty(s3Client, bucketId)) {
			throw new FileDownloadException("Requested bucket does not exist or is empty");
		}
		s3Client.deleteObject(bucketId, fileName);
		this.awsS3ClientBuilderService.shutdown(s3Client);
		return true;
	}

	public List<String> listFiles(String bucket) throws FileDownloadException {
		JsonNode keys = getSecrets(bucket);
		String bucketId = keys.get("bucket").asText();

		final AmazonS3 s3Client = awsS3ClientBuilderService.createAwsS3Client(keys.get("api_key").asText(),
				keys.get("secret_key").asText(), Regions.US_EAST_1);

		if (bucketIsEmpty(s3Client, bucketId)) {
			throw new FileDownloadException("Requested bucket does not exist or is empty");
		}
		ListObjectsRequest listObjectsRequest = new ListObjectsRequest().withBucketName(bucketId);

		ObjectListing objectListing;
		List<String> summaryList = new ArrayList<String>();
		do {
			objectListing = s3Client.listObjects(listObjectsRequest);
			for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
				summaryList.add(objectSummary.getKey());
			}
			listObjectsRequest.setMarker(objectListing.getNextMarker());
		} while (objectListing.isTruncated());
		this.awsS3ClientBuilderService.shutdown(s3Client);

		return summaryList;
	}

	private boolean bucketIsEmpty(AmazonS3 s3Client, String bucketName) {
		ListObjectsV2Result result = s3Client.listObjectsV2(bucketName);
		if (result == null) {
			return false;
		}
		List<S3ObjectSummary> objects = result.getObjectSummaries();
		return objects.isEmpty();
	}

	private String generateFileName(MultipartFile multiPart) {
		return new Date().getTime() + "-" + multiPart.getOriginalFilename().replace(" ", "_");
	}

	private JsonNode getSecrets(String key) {

		for (Entry<String, JsonNode> entry : FileMgmtApplication.vaultVariablesMap.entrySet()) {

			if (entry.getKey().contains(key))
				return entry.getValue();
		}
		return JsonNodeFactory.instance.objectNode();
	}

}

package com.convatec.filemgmt.util;

public class AppTestConstants {
    public static final String BUCKET = "testbucket";
    public static final String FILENAME = "testFile.txt";
    public static final String TEST= "test";
    public static final String CONTENT_TYPE = "text/plain";
    public static final String TEST_FILE_CONTENT = "Test file content";
    public static final boolean FALSE = false;
    public static final boolean TRUE = true;
    public static final String BUCKET_KEY = "bucket_key";
    public static final String COOLNAME = "CoolName";
    public static final String VAULT_VARIABLES_MAP = "vaultVariablesMap";

    public static final String JSON_CONTENT = "{\"api_key\":\"AKIQQN\",\"bucket\":\"cf-s3-d6072a6\",\"bucket_key\":\"testbucket\",\"secret_key\":\"tFvWE76fn8bta0l\"}";
    public static final String LIST_FILES_ERROR_STR = "Cannot invoke \"com.amazonaws.services.s3.AmazonS3.listObjectsV2(String)\" because \"s3Client\" is null";
    public static final String DOWNLOAD_FILE_ERROR_STR = "Cannot invoke \"com.amazonaws.services.s3.AmazonS3.listObjectsV2(String)\" because \"s3Client\" is null";
    public static final String DELETE_FILE_ERROR_STR = "Cannot invoke \"com.amazonaws.services.s3.AmazonS3.listObjectsV2(String)\" because \"s3Client\" is null";
    public static final String UPLOAD_FILE_ERROR_STR = "Cannot invoke \"com.amazonaws.services.s3.AmazonS3.putObject(com.amazonaws.services.s3.model.PutObjectRequest)\" because \"s3Client\" is null";
    public static final String SHUTDOWN_ERROR_STR = "Cannot invoke \"com.amazonaws.services.s3.AmazonS3.shutdown()\" because \"s3Client\" is null";
}
Leave a Comment