001    /*
002     * Copyright (c) 2009 The openGion Project.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *     http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
013     * either express or implied. See the License for the specific language
014     * governing permissions and limitations under the License.
015     */
016    package org.opengion.hayabusa.servlet;
017    
018    import java.io.File;
019    import java.io.IOException;
020    
021    import org.opengion.fukurou.util.FileUtil;
022    
023    /**
024     * ファイルをサーバ?にア??ロードする?合に使用されるファイル管?ラスです?
025     * HTML5 ファイルア??ロード??選択?ultiple?対?に伴???のクラスとして public化します?
026     *
027     * @og.group そ?他機?
028     * @og.rev 5.7.1.1 (2013/12/13) HTML5 ファイルア??ロード??選択?ultiple?対?
029     *
030     * @version  4.0
031     * @author       Kazuhiko Hasegawa
032     * @since    JDK5.0,
033     */
034    public final class UploadedFile implements Comparable<UploadedFile> {
035    
036            /** バッファの初期容量を通常より多い目に設定します?  {@value}  */
037            public static final int BUFFER_MIDDLE = 200;
038    
039            /** シス?依存?改行記号をセ?します?     */
040            public static final String CR = System.getProperty("line.separator");
041    
042            private File filename = null;           // 現時点での置き換え後ファイル?
043    
044            private final String uniqKey ;          // ア??ロードされたファイル?ユニ?クにしておきま?
045            private final String dir;
046            private final String name;
047            private final String original;
048            private final String type;
049    
050            /**
051             * ア??ロードファイルの管?ブジェクトを作?します?
052             *
053             * @og.rev 5.7.1.1 (2013/12/13) HTML5 ファイルア??ロード??選択?ultiple?対?
054             *
055             * @param       uniqKey         ユニ?クキー(初期ア??ロードファイル?
056             * @param       dir                     ファイルを保管するフォル?
057             * @param       name            ファイルア??ロードされた時?name属?
058             * @param       original        ファイル?オリジナル)
059             * @param       type    コン?トタイ?
060             */
061    //      UploadedFile( final String dir, final String name, final String filename, final String original, final String type) {
062            UploadedFile( final String uniqKey, final String dir, final String name, final String original, final String type ) {
063                    this.uniqKey    = uniqKey;              // 5.7.1.1 (2013/12/13) uniqKey を確定させる?
064                    this.dir                = dir;
065                    this.name               = name;
066    //              this.filename   = filename;
067                    this.original   = original;
068                    this.type               = type;
069            }
070    
071            /**
072             * ファイルア??ロードされた時?name属?を取得します?
073             *
074             * @og.rev 5.7.1.1 (2013/12/13) HTML5 ファイルア??ロード??選択?ultiple?対?
075             *
076             * @return      ファイルア??ロードされた時?name属?
077             */
078            public String getName() {
079                    return name;
080            }
081    
082            /**
083             * コン?トタイプを取得します?
084             *
085             * @return      コン?トタイ?
086             */
087            public String getContentType() {
088                    return type;
089            }
090    
091            /**
092             * ファイル?置き換え?を取得します?
093             *
094             * @og.rev 5.7.1.2 (2013/12/20) zip 対応で、Fileオブジェクトを返すようにします?
095             *
096             * @return      ファイル?置き換え?
097             */
098    //      public String getFilesystemName() {
099            public File getUploadFile() {
100                    return filename;
101            }
102    
103            /**
104             * ファイル?置き換え?の置き換えを実行します?
105             * useBackup = true にすると、dir の直下に?_backup" フォル?作?します?
106             * バックア??ファイル名?、?のファイル?拡張子含? ??"_" + 現在時刻のlong値 + "." + ??ファイルの拡張?
107             *
108             * newName ?null の場合?、original のファイル名に、変換します?
109             *
110             * @og.rev 5.7.1.1 (2013/12/13) 新規追?
111             * @og.rev 5.7.2.1 (2014/01/17) FileUtil.renameTo の引数の?を間違えて??
112             *
113             * @param       newName         ファイル?置き換え?
114             * @param       useBackup       置き換え後ファイルをバ?ア??するかど?(true:バックア??する/false:しな?
115             * @return      ?的に作?されたファイルオブジェク?
116             */
117            public File renameTo( final String newName , final boolean useBackup ) {
118    
119                    String newNm = newName ;
120                    // 新規ファイル名を作?します?(拡張子?
121                    if( newNm != null && newNm.length() > 0 ) {
122                            // 新ファイル名から拡張子取?
123                            String newExt = FileUtil.getExtension( newNm );
124                            if( newExt == null || newExt.length() == 0 ) {
125                                    String oldExt = FileUtil.getExtension( original );
126                                    newNm = newNm + "." + oldExt ;
127                            }
128                    }
129                    else {
130                            newNm = original;
131                    }
132    
133                    File newFile = null ;
134                    if( newNm != null && newNm.length() > 0 ) {
135                            newFile = new File( dir,newNm );
136    
137                            File uniqFile = new File( dir , uniqKey );              // 5.7.1.1 (2013/12/13) ア??ロードされたファイル
138    
139                            // 5.7.2.1 (2014/01/17) FileUtil.renameTo の引数の?を間違えて??
140    //                      FileUtil.renameTo( newFile, uniqFile , useBackup );
141                            FileUtil.renameTo( uniqFile , newFile, useBackup );             // from ?to の?に??
142    
143    //                      // 置き換えファイルの存在チェ??
144    //                      if( newFile.exists() ) {
145    //                              if( useBackup ) {
146    //                                      // newNm にフォル?層を含??合に、そなえて?
147    //                                      File parent = newFile.getParentFile();                  // バックア??すべきファイルのフォル?
148    //                                      File backup = new File( parent , "_backup" );   // そ?直下に?_backup" フォル?作?
149    //                                      if( !backup.exists() && !backup.mkdirs() ) {
150    //                                              String errMsg = "バックア??処?backupフォル??作?に失敗しました?" + backup + "]";
151    //                                              throw new RuntimeException( errMsg );
152    //                                      }
153    //                                      // バックア??ファイル名?、?のファイル?拡張子含? ??"_" + 現在時刻のlong値 + "." + ??ファイルの拡張?
154    //                                      String bkupName = newFile.getName() + "_" + System.currentTimeMillis() + "."  + FileUtil.getExtension( newNm ) ;
155    //                                      File fromFile = new File( dir,newNm );          // オリジナルの newFile をrename するとまずいので、同名?Fileオブジェクトを作?
156    //                                      File bkupFile = new File( backup,bkupName );
157    //
158    //                                      if( !fromFile.renameTo( bkupFile ) ) {
159    //                                              String errMsg = "バックア??処?バックア??ファイルをリネ??きませんでした? +CR
160    //                                                                                       + "  [" + fromFile + "] ?[" + bkupFile + "]" ;
161    //                                              throw new RuntimeException( errMsg );
162    //                                      }
163    //                              }
164    //                              else if( !newFile.delete() ) {
165    //                                      String errMsg = "既存?ファイル[" + newNm + "]が削除できませんでした?;
166    //                                      throw new RuntimeException( errMsg );
167    //                              }
168    //                      }
169    //
170    //                      File uniqFile = new File( dir , uniqKey );              // 5.7.1.1 (2013/12/13) ア??ロードされたファイル
171    //                      if( !uniqFile.renameTo( newFile ) ) {
172    //                              String errMsg = "??ファイルをリネ??きませんでした? + CR
173    //                                                                      + "  [" + uniqFile + "] ?[" + newFile + "]" ;
174    //                              throw new RuntimeException( errMsg );
175    //                      }
176                    }
177                    // 5.7.1.1 (2013/12/13) ここの処?走ることは無??ず?
178                    else {
179                            String errMsg = "新ファイル名が存在しません?" + newNm + "]" ;
180                            throw new RuntimeException( errMsg );
181                    }
182                    // 新ファイル名?セ?は、すべての処?完?てから、設定する?
183                    filename = newFile ;
184                    return filename;
185            }
186    
187            /**
188             * ファイル?置き換え?をセ?します?
189             *
190             * @og.rev 5.7.1.1 (2013/12/13) ?
191             *
192             * @param       name    ファイル?置き換え?
193             */
194    //      public void setFilesystemName( final String name ) {
195    //              filename = name;
196    //      }
197    
198            /**
199             * ファイル?オリジナル)を取得します?
200             *
201             * @return      ファイル?オリジナル)
202             */
203            public String getOriginalFileName() {
204                    return original;
205            }
206    
207            /**
208             * ファイル?置き換え?の File オブジェクトを取得します?
209             *
210             * @og.rev 5.7.1.1 (2013/12/13) ??
211             *
212             * @return Fileオブジェク?
213             */
214    //      public File getFile() {
215    //              if(dir == null || filename == null) {
216    //                      return null;
217    //              }
218    //              else {
219    //                      return new File(dir + File.separator + filename);
220    //              }
221    //      }
222    
223            /**
224             * ファイル名か?拡張子を取得します?
225             *
226             * @og.rev 5.7.1.1 (2013/12/13) ローカルに移動?若干のロジ?変更
227             *
228             * @param       fileName        ファイル?
229             * @return      拡張?
230             */
231    //      private String getExtension( final String fileName ) {
232    //              int index = fileName.lastIndexOf('.');
233    ////            if(index!=-1) {
234    ////                    return fileName.substring(index + 1, fileName.length());
235    ////            }
236    //              if( index >= 0 ) {
237    //                      return fileName.substring( index + 1 );
238    //              }
239    //              return "";
240    //      }
241    
242            /**
243             * 自然比?ソ?
244             * インタフェース Comparable の 実?関連して、?定義して?す?
245             * 登録されたシーケンス(画面の表示?で比?ます?
246             * equals メソ?では、キーの同??のみに?して判定して?す?
247             * こ?比?は?運用上同?ーは発生しません?たとえ同?ーが存在した
248             * としても?そ?比??が同じになることを保証して?せん?
249             *
250             * @param   other 比?象のObject
251             *
252             * @return  こ?オブジェクトが?されたオブジェクトより小さ??合???整数、等し??合?ゼロ、大きい場合?正の整数
253             * @throws  ClassCastException 引数?UploadedFile ではな???
254             * @throws  IllegalArgumentException 引数?null の場?
255             */
256            @Override
257            public int compareTo( final UploadedFile other ) {
258                    if( other == null ) {
259                            String errMsg = "引数が?null です?" ;
260                            throw new IllegalArgumentException( errMsg );
261                    }
262    
263                    return ( uniqKey ).compareTo( other.uniqKey );
264            }
265    
266            /**
267             * こ?オブジェクトと他?オブジェクトが等し?ど?を示します?
268             * 画面は、画面IDが等しければ、?や表示?関係なく同?みなされます?
269             * GUIInfo は、ユーザー個別に扱われ、そのグループには、key は唯?、かつ
270             * 同???で扱われるオブジェクト?為、同?みなします?
271             *
272             * @param   object 比?象の参?オブジェク?
273             *
274             * @return      引数に?されたオブジェクトとこ?オブジェクトが等し??合? true、そ?な??合? false
275             */
276            @Override
277            public boolean equals( final Object object ) {
278                    if( object instanceof UploadedFile ) {
279                            return uniqKey.equals( ((UploadedFile)object).uniqKey );
280                    }
281    
282                    return false ;
283            }
284    
285            /**
286             * オブジェクト?ハッシュコード?を返します?
287             * こ?メソ?は、java.util.Hashtable によって提供されるような
288             * ハッシュ??ブルで使用するために用意されて?す?
289             * equals( Object ) メソ?をオーバ?ライトした?合?、hashCode() メソ??
290             * ? 記述する?があります?
291             * こ?実?は、getKey().hashCode() と同?を返します?
292             *
293             * @return  こ?オブジェクト?ハッシュコード?
294             */
295            @Override
296            public int hashCode() {
297                    return uniqKey.hashCode() ;
298            }
299    
300            /**
301             * オブジェクト?識別子として?詳細な画面??を返します?
302             *
303             * @return  詳細な画面??
304             */
305            @Override
306            public String toString() {
307                    StringBuilder rtn = new StringBuilder( BUFFER_MIDDLE );
308                    rtn.append( this.getClass().getName()                   ).append( CR );
309                    rtn.append( "  uniqKey  :").append( uniqKey             ).append( CR );
310                    rtn.append( "  filename :").append( filename    ).append( CR );
311                    rtn.append( "  name     :").append( name                ).append( CR );
312                    rtn.append( "  dir      :").append( dir                 ).append( CR );
313                    rtn.append( "  original :").append( original    ).append( CR );
314                    rtn.append( "  type     :").append( type                ).append( CR );
315                    return rtn.toString();
316            }
317    }