package org.sagebionetworks.repo.manager.file;

import java.util.UUID;

import org.sagebionetworks.StackConfigurationSingleton;
import org.sagebionetworks.repo.model.project.ExternalGoogleCloudStorageLocationSetting;
import org.sagebionetworks.repo.model.project.ExternalS3StorageLocationSetting;
import org.sagebionetworks.repo.model.project.S3StorageLocationSetting;
import org.sagebionetworks.repo.model.project.StorageLocationSetting;
import org.springframework.util.StringUtils;

public class MultipartUtils {

	// [base/]userid/UUID/filename
	public static final String FILE_TOKEN_TEMPLATE_SEPARATOR = "/";
	private static final String FILE_TOKEN_TEMPLATE = "%1$s%2$s" + FILE_TOKEN_TEMPLATE_SEPARATOR + "%3$s" + FILE_TOKEN_TEMPLATE_SEPARATOR
			+ "%4$s";
	
	/**
	 * Lookup the bucket to use given a storage location id.
	 * @param storageLocationSetting
	 * @return
	 */
	public static String getBucket(StorageLocationSetting storageLocationSetting) {
		String bucket;
		if (storageLocationSetting == null || storageLocationSetting instanceof S3StorageLocationSetting) {
			bucket = StackConfigurationSingleton.singleton().getS3Bucket();
		} else if (storageLocationSetting instanceof ExternalS3StorageLocationSetting) {
			bucket = ((ExternalS3StorageLocationSetting) storageLocationSetting).getBucket();
		} else if (storageLocationSetting instanceof ExternalGoogleCloudStorageLocationSetting) {
			bucket = ((ExternalGoogleCloudStorageLocationSetting) storageLocationSetting).getBucket();
		} else {
			throw new IllegalArgumentException("Cannot get bucket from storage location setting type " + storageLocationSetting.getClass());
		}
		return bucket;
	}
	
	/**
	 * Create a new S3 key given a file name and storage location settings.s
	 * @param userId
	 * @param fileName
	 * @param storageLocationSetting
	 * @return
	 */
	public static String createNewKey(String userId, String fileName, StorageLocationSetting storageLocationSetting) {
		String base = "";
		if (storageLocationSetting instanceof ExternalS3StorageLocationSetting) {
			ExternalS3StorageLocationSetting externalS3StorageLocationSetting = (ExternalS3StorageLocationSetting) storageLocationSetting;
			if (!StringUtils.isEmpty(externalS3StorageLocationSetting.getBaseKey())) {
				// PLFM-5769: S3 base keys must end with only one trailing slash, but may be stored with one or more
				base = externalS3StorageLocationSetting.getBaseKey();
				while (base.endsWith("/")){
					// TODO: Migrate existing base keys to not have trailing slashes, delete this loop
					base = base.substring(0, base.length() - 1);
				}
				base += FILE_TOKEN_TEMPLATE_SEPARATOR;
			}
		} else if (storageLocationSetting instanceof ExternalGoogleCloudStorageLocationSetting) {
			ExternalGoogleCloudStorageLocationSetting externalGoogleCloudStorageLocationSetting = (ExternalGoogleCloudStorageLocationSetting) storageLocationSetting;
			if (!StringUtils.isEmpty(externalGoogleCloudStorageLocationSetting.getBaseKey())) {
				base = externalGoogleCloudStorageLocationSetting.getBaseKey() + FILE_TOKEN_TEMPLATE_SEPARATOR;
			}
		}
		return String.format(FILE_TOKEN_TEMPLATE, base, userId, UUID.randomUUID().toString(), fileName);
	}
	
	
}

@@ -2305,360 +2305,62 @@
 %09%09%09%09
-// PLFM-5769: S3 base keys must end with only one trailing slash, but may be stored with one or more%0D%0A%09%09%09%09base = externalS3StorageLocationSetting.getBaseKey();%0D%0A%09%09%09%09while (base.endsWith(%22/%22))%7B%0D%0A%09%09%09%09%09// TODO: Migrate existing base keys to not have trailing slashes, delete this loop%0D%0A%09%09%09%09%09base = base.substring(0, base.length() - 1);%0D%0A%09%09%09%09%7D%0D%0A%09%09%09%09base
+base = externalS3StorageLocationSetting.getBaseKey()
  +
-=
  FIL

Return the createNewKey function to its original form