1 18 package org.drftpd.commands; 19 20 import java.io.IOException ; 21 import java.util.Collection ; 22 import java.util.Comparator ; 23 import java.util.Iterator ; 24 import java.util.TreeSet ; 25 26 import net.sf.drftpd.master.BaseFtpConnection; 27 import net.sf.drftpd.master.FtpReply; 28 import net.sf.drftpd.master.command.plugins.Textoutput; 29 import net.sf.drftpd.master.command.CommandHandlerBundle; 30 import net.sf.drftpd.master.command.CommandManager; 31 import net.sf.drftpd.master.command.CommandManagerFactory; 32 import net.sf.drftpd.remotefile.LinkedRemoteFileInterface; 33 import net.sf.drftpd.Bytes; 34 35 import org.apache.log4j.Logger; 36 import org.drftpd.sections.SectionInterface; 37 import org.tanesha.replacer.ReplacerEnvironment; 38 39 43 public class New implements CommandHandlerBundle { 44 private static final Logger logger = Logger.getLogger(New.class); 45 46 private class DateComparator implements Comparator { 47 public int compare(Object o1, Object o2) { 48 if (!(o1 instanceof LinkedRemoteFileInterface 49 && o2 instanceof LinkedRemoteFileInterface)) { 50 throw new ClassCastException ("Not a LinkedRemoteFile"); 51 } 52 53 LinkedRemoteFileInterface f1 = (LinkedRemoteFileInterface) o1; 54 LinkedRemoteFileInterface f2 = (LinkedRemoteFileInterface) o2; 55 56 if (f1.lastModified() == f2.lastModified()) { 57 return 0; 58 } 59 60 return f1.lastModified() > f2.lastModified() ? -1 : 1; 61 } 62 63 public boolean equals(Object o) { 64 if (!(o instanceof DateComparator)) { 65 return false; 66 } 67 68 return super.equals(o); 69 } 70 } 71 72 public New() { 73 super(); 74 } 75 76 public FtpReply execute(BaseFtpConnection conn) 77 throws UnhandledCommandException { 78 FtpReply reply = new FtpReply(200); 79 Collection sections = 80 conn.getConnectionManager().getSectionManager().getSections(); 81 int count = 20; 82 try { 83 count = Integer.parseInt(conn.getRequest().getArgument()); 84 } catch (Exception e) { 85 } 87 88 91 Collection directories = new TreeSet (new DateComparator()); 92 for (Iterator iter = sections.iterator(); iter.hasNext();) { 93 SectionInterface section = (SectionInterface) iter.next(); 94 directories.addAll(section.getFile().getDirectories()); 95 } 96 97 try { 98 Textoutput.addTextToResponse(reply, "new_header"); 99 } catch (IOException ioe) { 100 logger.warn("Error reading new_header", ioe); 101 } 102 103 ReplacerEnvironment env = new ReplacerEnvironment(); 105 int pos = 1; 106 long now = System.currentTimeMillis(); 107 for (Iterator iter = directories.iterator(); 108 iter.hasNext() && pos <= count; 109 pos++) { 110 LinkedRemoteFileInterface dir = 111 (LinkedRemoteFileInterface) iter.next(); 112 env.add("pos", "" + pos); 113 env.add("name", dir.getName()); 114 env.add("diruser", dir.getUsername()); 115 env.add("files", "" + dir.dirSize()); 116 env.add("size", Bytes.formatBytes(dir.length())); 117 env.add("age", "" + formatAge(dir.lastModified(), now)); 118 reply.addComment(conn.jprintf(New.class, "new", env)); 119 } 120 121 try { 122 Textoutput.addTextToResponse(reply, "new_footer"); 123 } catch (IOException ioe) { 124 logger.warn("Error reading new_footer", ioe); 125 } 126 127 return reply; 128 } 129 130 private static final String formatAge(long age, long now) { 131 if (age >= now) { 132 return "0m 0s"; 133 } 134 135 if (now - age < 60 * 60 * 1000) { 137 long min = (now - age) / 60000; 138 long s = ((now - age) - min * 60000) / 1000; 139 return min + "m " + (s > 9 ? "" + s : " " + s) + "s"; 140 } 141 142 if (now - age < 24 * 60 * 60 * 1000) { 144 long h = (now - age) / (60 * 60000); 145 long min = ((now - age) - h * 60 * 60000) / 60000; 146 return h + "h " + (min > 9 ? "" + min : " " + min) + "m"; 147 } 148 149 long d = (now - age) / (24 * 60 * 60000); 151 long h = ((now - age) - d * 24 * 60 * 60000) / (60 * 60000); 152 return d + "d " + (h > 9 ? "" + h : " " + h) + "h"; 153 } 154 155 public CommandHandler initialize( 156 BaseFtpConnection conn, 157 CommandManager initializer) { 158 return this; 159 } 160 161 public String [] getFeatReplies() { 162 return null; 163 } 164 165 public void load(CommandManagerFactory initializer) { 166 } 167 168 public void unload() { 169 } 170 171 } 172 | Popular Tags |