001
014
015 package com.liferay.portlet.documentlibrary.store;
016
017 import com.liferay.portal.kernel.memory.DeleteFileFinalizeAction;
018 import com.liferay.portal.kernel.memory.FinalizeManager;
019 import com.liferay.portal.kernel.util.FileUtil;
020
021 import java.io.File;
022 import java.io.FileInputStream;
023 import java.io.InputStream;
024
025 import org.aopalliance.intercept.MethodInterceptor;
026 import org.aopalliance.intercept.MethodInvocation;
027
028
031 public class TempFileMethodInterceptor implements MethodInterceptor {
032
033 @Override
034 public Object invoke(MethodInvocation methodInvocation) throws Throwable {
035 Object result = methodInvocation.proceed();
036
037 if (result instanceof InputStream) {
038 InputStream inputStream = (InputStream)result;
039
040 File tempFile = FileUtil.createTempFile(inputStream);
041
042 result = new FileInputStream(tempFile);
043
044 FinalizeManager.register(
045 result,
046 new DeleteFileFinalizeAction(tempFile.getAbsolutePath()));
047 }
048
049 return result;
050 }
051
052 }