001
002 package com.rain;
003
004 import java.io.*;
005 import java.util.*;
006
007 /**
008 * ÷ºÎ ÆÄÀÏ °ü¸® ¹× ±âŸ »ý¼ºµÈ Àӽà ÆÄÀÏ °ü¸®
009 * @version 1.0
010 * @author http://cafen.net (outmind@cafen.net)
011 */
012 public class rainFileManager {
013
014 /**
015 * °ü¸®ÇÒ ÆÄÀÏ °æ·Î
016 */
017 public String base_file = "";
018
019 /**
020 * ȯ°æ ÆÄÀÏ °æ·Î
021 */
022 public String base_conf = "";
023
024 /**
025 * ±âº» ÀÛ¾÷ °ø°£
026 */
027 public String base_root = "";
028
029 /**
030 * Àӽà ÆÄÀÏÀÇ ÀÛ¾÷°ø°£
031 */
032 public String base_cashed = "";
033
034 /**
035 * »ý¼ºÀÚ
036 * @param fileName °ü¸®ÇÒ ÆÄÀϸí
037 */
038 public rainFileManager(String fileName){
039 this.base_file = fileName;
040 if (rainUtil.isMovie(this.base_file))
041 this.base_conf = "movie/" + this.base_file+".ini";
042 else if (rainUtil.isImage(this.base_file))
043 this.base_conf = "thumb/" + this.base_file+".ini";
044 else
045 this.base_conf = "thumb/" + this.base_file+".ini";
046 this.base_root = rainUtil.getConf("path_uploads");
047 }
048
049 /**
050 * °ü·Ã ÆÄÀÏ Ãß°¡
051 * @param fileName Ãß°¡ÇÒ °ü·Ã ÆÄÀÏ
052 */
053 public void add(String fileName) {
054 try {
055 if (rainUtil.checkDir(this.base_root, this.base_conf)) {
056 fileName = (new File(fileName)).getName();
057 FileWriter fout = new FileWriter(new File(this.base_root + this.base_conf), true);
058 BufferedWriter bout = new BufferedWriter(fout);
059 bout.write(fileName + "#");
060 bout.close();
061 fout.close();
062 }
063 } catch(Exception e) {
064 rainUtil.errorLog(e.getMessage());
065 }
066 }
067
068 /**
069 * Àӽà ¿µ¿ª Æúµå °¡Á®¿À±â
070 * @return Àӽÿµ¿ª Æúµå¸í
071 */
072 public String getCashDir() {
073 return this.getCashDir(this.base_file);
074 }
075
076 /**
077 * Àӽà ¿µ¿ª Æúµå °¡Á®¿À±â
078 * @param fileName ´ë»ó ÆÄÀϸí
079 * @return Àӽÿµ¿ª Æúµå¸í
080 */
081 public String getCashDir(String fileName) {
082 String cash_dir = "";
083 if (rainUtil.isMovie(fileName))
084 cash_dir = (new File("movie/" + fileName+"/")).getParent();
085 else if (rainUtil.isImage(fileName))
086 cash_dir = (new File("thumb/" + fileName+"/")).getParent();
087 return cash_dir;
088 }
089
090 /**
091 * »ý¼ºµÈ Àӽà ¿µ¿ªÀÇ ÆÄÀÏ °¡Á®¿À±â
092 * @return Àӽà »ý¼ºµÈ ÆÄÀÏ
093 */
094 public String[] readCashed() {
095 String file_str = "";
096 if ((new File(this.base_root + this.base_conf)).exists()) {
097 try {
098 BufferedReader input = new BufferedReader (new FileReader (new File(this.base_root + this.base_conf)));
099 String nextLine = "";
100 while ((nextLine = input.readLine()) != null ) file_str += nextLine;
101 input.close();
102 } catch(Exception e) {}
103 }
104 return file_str.split("#");
105 }
106
107 /**
108 * Àӽà ÆÄÀÏ »èÁ¦
109 */
110 public void deleteJunk() {
111 String real_path = "junk/";
112 File[] filelist =(new File(this.base_root+real_path).listFiles());
113 int base_time = rainUtil.getNow() - 60*60*2;
114 for(int i =0; i < filelist.length; i++) {
115 if (filelist[i].isFile() && Math.round(filelist[i].lastModified()/(long)1000) < base_time) {
116 rainFileManager junkmanager = new rainFileManager(real_path + filelist[i].getName());
117 junkmanager.delete(true);
118 }
119 }
120 }
121
122 /**
123 * ÆÄÀÏ »èÁ¦
124 */
125 public void delete() {
126 this.delete(false);
127 }
128
129 /**
130 * ÆÄÀÏ »èÁ¦
131 * @param isReal ½ÇÁ¦»èÁ¦ ¿©ºÎ(true:½ÇÁ¦»èÁ¦µÊ, false:°ü·Ã ¸ðµç ÆÄÀÏ¸í µÚ¿¡ _ À» ºÙÀÓ) false ·Î »èÁ¦µÈ °æ¿ì º¹±¸°¡´É
132 */
133 public void delete(boolean isReal) {
134 try {
135 if (isReal) {
136 if ((new File(this.base_root + this.base_file)).exists())
137 (new File(this.base_root + this.base_file)).delete();
138 String cash_dir = this.getCashDir();
139 if (!cash_dir.equals("")) {
140 String[] fileList = this.readCashed();
141 for(int i = 0; i < fileList.length; i++) {
142 if (!fileList[i].equals("")) {
143 String cashedName = this.base_root + cash_dir + "/" + fileList[i];
144 if ((new File(cashedName)).exists())
145 (new File(cashedName)).delete();
146 }
147 }
148 }
149 if ((new File(this.base_root + this.base_conf)).exists())
150 (new File(this.base_root + this.base_conf)).delete();
151 } else {
152 if ((new File(this.base_root + this.base_file)).exists())
153 (new File(this.base_root + this.base_file)).renameTo(new File( this.base_root + this.base_file + "_"));
154 String cash_dir = this.getCashDir();
155 if (!cash_dir.equals("")) {
156 String[] fileList = this.readCashed();
157 for(int i = 0; i < fileList.length; i++) {
158 if (!fileList[i].equals("")) {
159 String cashedName = this.base_root + cash_dir + "/" + fileList[i];
160 if ((new File(cashedName)).exists())
161 (new File(cashedName)).renameTo((new File(cashedName+ "_")));
162 }
163 }
164 }
165 if ((new File(this.base_root + this.base_conf)).exists())
166 (new File(this.base_root + this.base_conf)).renameTo(new File(this.base_root + this.base_conf+ "_"));
167 }
168 if (!rainUtil.find("^junk/",this.base_file) && (new Random()).nextInt(10) == 0)
169 this.deleteJunk();
170
171 } catch(Exception e) {}
172 }
173
174 /**
175 * ÆÄÀÏ ÀÌÀü
176 * @param dirName ÀÌÀüÇÒ ÆÄÀÏ °æ·Î
177 */
178 public void move(String dirName) {
179 try {
180 if (rainUtil.checkDir(this.base_root, dirName)) {
181 dirName = (new File(dirName + "/")).getParent();
182 String baseName = (new File(this.base_file)).getName();
183 if ((new File(this.base_root + this.base_file)).exists())
184 (new File(this.base_root + this.base_file)).renameTo((new File( this.base_root + dirName + "/" + baseName)));
185 String cash_dir = this.getCashDir();
186 String ncash_dir = this.getCashDir(dirName + "/" + baseName);
187 if (!cash_dir.equals("") && !ncash_dir.equals("")) {
188 if (rainUtil.checkDir(this.base_root, ncash_dir + "/" + baseName)) {
189 String[] fileList = this.readCashed();
190 for(int i = 0; i < fileList.length; i++) {
191 if (!fileList[i].equals("")) {
192 String cashedName = this.base_root + cash_dir + "/" + fileList[i];
193 String ncashedName = this.base_root + ncash_dir + "/" + fileList[i];
194 if ((new File(cashedName)).exists())
195 (new File(cashedName)).renameTo((new File(ncashedName)));
196 }
197 }
198 }
199 }
200
201 if ((new File(this.base_root + this.base_conf)).exists())
202 (new File(this.base_root + this.base_conf)).renameTo((new File( this.base_root + ncash_dir + "/" + baseName + ".ini")));
203 }
204 } catch(Exception e) {}
205 }
206
207 /**
208 * ÆÄÀÏ º¹±¸ - delete(false) ·Î »èÁ¦µÈ °æ¿ì¸¸ º¹±¸µÊ
209 */
210 public void restore() {
211 try {
212 if ((new File(this.base_root + this.base_file + "_")).exists())
213 (new File(this.base_root + this.base_file + "_")).renameTo(new File( this.base_root + this.base_file));
214 if ((new File(this.base_root + this.base_conf + "_")).exists())
215 (new File(this.base_root + this.base_conf + "_")).renameTo(new File(this.base_root + this.base_conf));
216 String cash_dir = this.getCashDir();
217 if (!cash_dir.equals("")) {
218 String[] fileList = this.readCashed();
219 for(int i = 0; i < fileList.length; i++) {
220 if (!fileList[i].equals("")) {
221 String cashedName = this.base_root + cash_dir + "/" + fileList[i];
222 if ((new File(cashedName + "_")).exists())
223 (new File(cashedName + "_")).renameTo((new File(cashedName)));
224 }
225 }
226 }
227 } catch(Exception e) {}
228 }
229 }