Subversion Repositories bacoAlunos

Compare Revisions

Ignore whitespace Rev 1350 → Rev 1353

/branches/v3/impl/src/java/pt/estgp/estgweb/services/ftpservices/FtpService.java
2,16 → 2,23
 
import jomm.utils.DesUtils;
import jomm.utils.DiacriticFilter;
import jomm.utils.FilesUtils;
import jomm.utils.MimeTypeGuesser;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.httpclient.util.URIUtil;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONObject;
import pt.estgp.estgweb.domain.CourseUnitImpl;
import pt.estgp.estgweb.domain.UserSession;
import pt.estgp.estgweb.domain.UserSessionImpl;
import pt.estgp.estgweb.domain.dao.DaoFactory;
import pt.estgp.estgweb.utils.ConfigProperties;
import pt.estgp.estgweb.web.FtpServer;
import pt.estgp.estgweb.web.utils.DatesUtils;
import pt.utl.ist.berserk.logic.serviceManager.IService;
 
import java.io.File;
158,4 → 165,143
client.disconnect();
return result;
}
 
private static void printMem()
{
int mb = 1024*1024;
 
//Getting the runtime reference from system
Runtime runtime = Runtime.getRuntime();
 
 
//Print used memory
logger.debug("Used Memory:" + (runtime.totalMemory() - runtime.freeMemory()) / mb);
 
}
public static long[] calculateDirectoryInfo(FtpServer.FtpClientWrapper ftpClient,
String parentDir, String currentDir) throws IOException {
long[] info = new long[4];
//System.gc();
printMem();
int totalDirs = 0;
int totalFiles = 0;
long totalSize = 0;
long lastUpdate = 0;
 
String dirToList = parentDir;
if (!currentDir.equals("")) {
dirToList += "/" + currentDir;
}
 
try {
 
FTPFile[] subFiles = ftpClient.listFiles(dirToList);
 
if (subFiles != null && subFiles.length > 0) {
for (FTPFile aFile : subFiles) {
String currentFileName = aFile.getName();
if (currentFileName.equals(".")
|| currentFileName.equals("..")) {
// skip parent directory and the directory itself
continue;
}
 
//System.out.println("dir:" +currentDir+ aFile.getName());
if (aFile.isDirectory()) {
//System.out.println("dir:" +currentDir+ aFile.getName());
totalDirs++;
long[] subDirInfo =
calculateDirectoryInfo(ftpClient, dirToList, currentFileName);
totalDirs += subDirInfo[0];
totalFiles += subDirInfo[1];
totalSize += subDirInfo[2];
if(subDirInfo[3] > lastUpdate)
lastUpdate = subDirInfo[3];
} else {
totalSize += aFile.getSize();
totalFiles++;
if(aFile.getTimestamp().getTime().getTime() > lastUpdate)
lastUpdate = aFile.getTimestamp().getTime().getTime();
 
}
}
}
 
info[0] = totalDirs;
info[1] = totalFiles;
info[2] = totalSize;
info[3] = lastUpdate;
 
return info;
} catch (Throwable ex) {
logger.error(ex,ex);
return info;
}
}
 
 
public static JSONArray getDirectoryFilesFields(FtpServer.FtpClientWrapper ftpClient,
String parentDir, String currentDir) throws IOException {
return getDirectoryFilesFields(ftpClient, parentDir, currentDir,parentDir);
}
 
private static JSONArray getDirectoryFilesFields(FtpServer.FtpClientWrapper ftpClient,
String parentDir, String currentDir, String startDir) throws IOException {
 
JSONArray subArray = new JSONArray();
 
printMem();
 
String dirToList = parentDir;
if (!currentDir.equals("")) {
dirToList += "/" + currentDir;
}
 
try {
 
FTPFile[] subFiles = ftpClient.listFiles(dirToList);
 
if (subFiles != null && subFiles.length > 0) {
for (FTPFile aFile : subFiles) {
String currentFileName = aFile.getName();
if (currentFileName.equals(".")
|| currentFileName.equals("..")) {
// skip parent directory and the directory itself
continue;
}
 
//System.out.println("dir:" +currentDir+ aFile.getName());
if (aFile.isDirectory()) {
//System.out.println("dir:" +currentDir+ aFile.getName());
JSONArray subDirInfo =
getDirectoryFilesFields(ftpClient, dirToList, currentFileName,startDir);
for(int i = 0; i < subDirInfo.length();i++)
subArray.put(subDirInfo.getJSONObject(i));
 
} else {
JSONObject jsonObject = new JSONObject();
jsonObject.put("fileCompletePath",parentDir + "/" + currentDir + "/" + aFile.getName());
jsonObject.put("fileCompletePathEncoded",URIUtil.encodePath(parentDir + "/" + currentDir + "/" + aFile.getName(), "ISO-8859-1"));
 
jsonObject.put("file",parentDir.substring(startDir.length()) + "/" + currentDir + "/" + aFile.getName());
jsonObject.put("filename",aFile.getName());
jsonObject.put("contenttype", MimeTypeGuesser.getInstance().guessMimeType(aFile.getName()));
jsonObject.put("size",aFile.getSize());
jsonObject.put("lastmodified",aFile.getTimestamp().getTime().getTime());
jsonObject.put("lastmodifieddate", DatesUtils.getStringFromDate(aFile.getTimestamp().getTime()));
jsonObject.put("extension", FilesUtils.getExtension(aFile.getName()));
 
 
subArray.put(jsonObject);
}
}
}
 
 
return subArray;
} catch (Throwable ex) {
logger.error(ex,ex);
return subArray;
}
}
}