All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.antelopeframework.web.mvc.multipart.MultipartFileUtils Maven / Gradle / Ivy

The newest version!
package com.github.antelopeframework.web.mvc.multipart;

import static java.lang.String.format;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;
import org.springframework.web.multipart.MultipartFile;

import com.github.antelopeframework.web.mvc.ErrorWrapper;

/**
 * 
 * @author yangzhi
 *
 */
public abstract class MultipartFileUtils {
	private static final String UID = UUID.randomUUID().toString().replace('-', '_');
	private static final AtomicInteger COUNTER = new AtomicInteger(0);
	
	public final static class MultipartFileValidation {
		List errors = new ArrayList<>();
		
		private MultipartFileValidation() {
			//DO-NOTHING
		}
		
		/**
		 * 
		 * @param attach
		 * @param field
		 * @param message
		 * @return
		 */
		public MultipartFileValidation rejectIfEmpty(MultipartFile attach, String field, String message) {
			Assert.notNull(errors, "Errors object must not be null");
			
			if (attach == null) {
				errors.add(new ErrorWrapper(field, message));
			}
			
			return this;
		}
		
		/**
		 * 
		 * @param attach
		 * @param suffixs
		 * @param field
		 * @param message
		 * @return
		 */
		public MultipartFileValidation rejectIfNotSuffixWith(MultipartFile attach, String[] suffixs, String field, String message) {
			if (attach == null || suffixs == null || suffixs.length == 0) {
				return this;
			}
			
			String name = attach.getOriginalFilename();
			boolean v = false;
			for (String suffix : suffixs) {
				if (StringUtils.endsWithIgnoreCase(name, suffix)) {
					v = true;
					break;
				}
			}
			
			if (!v) {
				errors.add(new ErrorWrapper(field, message));
			}
			
			return this;
		}
		
		/**
		 * 
		 * @param attach
		 * @param size
		 * @param field
		 * @param message
		 * @return
		 */
		public MultipartFileValidation rejectIfBigThan(MultipartFile attach, long size, String field, String message) {
			if (attach == null) {
				return this;
			}
			
			if (attach.getSize() > size) {
				errors.add(new ErrorWrapper(field, message));
			}
			
			return this;
		}
		
		/**
		 * 
		 * @param flag
		 * @param field
		 * @param message
		 * @return
		 */
		public MultipartFileValidation rejectIfFalse(Boolean flag, String field, String message) {
			if (Boolean.FALSE.equals(flag)) {
				errors.add(new ErrorWrapper(field, message));
			}
			
			return this;
		}
		
		/**
		 * 
		 */
		public void validate() {
			if (errors.size() > 0) {
				throw new MultipartFileValidationException(errors);
			}
		}
	}
	
	/**
	 * 
	 * @return
	 */
	public static MultipartFileValidation validation() {
		return new MultipartFileValidation();
	}
	
	/**
	 * 
	 * @param attach
	 * @return
	 */
	public static File getAttachFile(MultipartFile attach) {
		if (attach == null) {
			return null;
		}

		File tempDir = new File(System.getProperty("java.io.tmpdir"));
		String tempFileName = format("upload_%s_%s.tmp", UID, getUniqueId());
		
		File dest = new File(tempDir, tempFileName);
		try {
			attach.transferTo(dest);
		} catch (IllegalStateException e) {
			//DO-NOTHING
		} catch (IOException e) {
			//DO-NOTHING
		}
		
		return dest;
	}
	
	/**
	 * 
	 * @param attach
	 * @return
	 */
	public static String getAttachFileName(MultipartFile attach) {
		if (attach == null) {
			return null;
		}
		
		return attach.getOriginalFilename();
	}
	
	/**
	 * 
	 * @return
	 */
	private static String getUniqueId() {
		final int limit = 100000000;
		int current = COUNTER.getAndIncrement();
		String id = Integer.toString(current);

		// If you manage to get more than 100 million of ids, you'll
		// start getting ids longer than 8 characters.
		if (current < limit) {
			id = ("00000000" + id).substring(id.length());
		}
		
		return id;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy