001
014
015 package com.liferay.portlet.documentlibrary.antivirus;
016
017 import com.liferay.portal.kernel.exception.SystemException;
018 import com.liferay.portal.kernel.util.FileUtil;
019
020 import java.io.File;
021 import java.io.IOException;
022 import java.io.InputStream;
023
024
027 public abstract class BaseFileAntivirusScanner implements AntivirusScanner {
028
029 @Override
030 public boolean isActive() {
031 return _ACTIVE;
032 }
033
034 @Override
035 public void scan(byte[] bytes)
036 throws AntivirusScannerException, SystemException {
037
038 File file = null;
039
040 try {
041 file = FileUtil.createTempFile(_ANTIVIRUS_EXTENSION);
042
043 FileUtil.write(file, bytes);
044
045 scan(file);
046 }
047 catch (IOException ioe) {
048 throw new SystemException("Unable to write temporary file", ioe);
049 }
050 finally {
051 if (file != null) {
052 file.delete();
053 }
054 }
055 }
056
057 @Override
058 public void scan(InputStream inputStream)
059 throws AntivirusScannerException, SystemException {
060
061 File file = null;
062
063 try {
064 file = FileUtil.createTempFile(_ANTIVIRUS_EXTENSION);
065
066 FileUtil.write(file, inputStream);
067
068 scan(file);
069 }
070 catch (IOException ioe) {
071 throw new SystemException("Unable to write temporary file", ioe);
072 }
073 finally {
074 if (file != null) {
075 file.delete();
076 }
077 }
078 }
079
080 private static final boolean _ACTIVE = true;
081
082 private static final String _ANTIVIRUS_EXTENSION = "avs";
083
084 }