001package org.opengion.fukurou.model;
002
003import java.io.File;
004import java.io.FileInputStream;
005import java.io.FileNotFoundException;
006import java.io.IOException;
007import java.io.InputStream;
008import java.nio.file.Files;
009import java.nio.file.Paths;
010import java.nio.file.StandardCopyOption;
011
012/**
013 * ファイル操作のインタフェース
014 * 
015 * ローカルサーバ、クラウドストレージ(AWS,AZURE,BLUEMIX,ORACLE)のファイル操作用です。
016 * FileOperationFactoryを通して、インスタンスを生成可能です。
017 * Fileクラスを継承しているため、通常のFileとしても扱えます。
018 * 
019 * @og.group ファイル操作
020 *
021 * @og.rev 5.10.8.0 (2019/02/01) 新規作成
022 * @og.rev 5.10.9.0 (2019/03/01) 変更対応
023 * @author oota
024 * @since       JDK7.0
025 */
026public class FileOperation extends File{
027        private String myplugin;
028        private String mybucket;
029        
030        /**
031         * コンストラクタ
032         * 
033         * 初期化処理。
034         * 
035         * @param path ファイルパス
036         */
037        public FileOperation(String path) {
038                super(path);
039        }
040        
041        /**
042         * コンストラクタ
043         * 
044         * FileOperationクラスでは、buketは使用しません。
045         * 
046         * @param bucket バケット名
047         * @param path ファイルパス
048         */
049        public FileOperation(String bucket, String path) {
050                this(path);
051                this.mybucket = bucket;
052        }
053
054        /**
055         * 書き込み処理
056         * 
057         * InputStreamのデータを書き込みます。
058         * 
059         * @param is 書き込みデータのInputStream
060         * @throws IOException ファイル関連エラー情報
061         */
062        public void write(final InputStream is) throws IOException {
063                // InpustStreamを対象パスに出力
064                Files.copy(is, Paths.get(this.getPath()), StandardCopyOption.REPLACE_EXISTING);
065        }
066
067        /**
068         * 読み込み処理
069         * 
070         * データを読み込み、InputStreamとして、返します。
071         * 
072         * @return 読み込みデータのInputStream
073         * @throws FileNotFoundException ファイル非存在エラー情報
074         */
075        public InputStream read() throws FileNotFoundException {
076                return new FileInputStream(this.getPath());
077        }
078
079        /**
080         * コピー処理
081         * 
082         * ファイルを指定先にコピーします。
083         * 
084         * @param afPath コピー先
085         * @return 成否フラグ
086         */
087        public boolean copy(String afPath) {
088                boolean flgRtn = false;
089
090                try {
091                        // 指定パスのファイルを、指定先にコピー from;jdk7
092                        Files.copy(Paths.get(this.getPath()), Paths.get(afPath), StandardCopyOption.REPLACE_EXISTING);
093                        flgRtn = true;
094                } catch (IOException ie) {
095                        // スルーしてfalseを返す
096                }
097
098                return flgRtn;
099        }
100
101        /**
102         * ファイル移動
103         * 
104         * ファイルを指定先に移動します。
105         * 
106         * @param afPath 移動先
107         * @return 成否フラグ
108         */
109        public boolean move(String afPath) {
110                boolean flgRtn = false;
111
112                try {
113                        // 指定パスのファイルを、指定先に移動 from:jdk7
114                        Files.move(Paths.get(this.getPath()), Paths.get(afPath), StandardCopyOption.REPLACE_EXISTING);
115                        flgRtn = true;
116                } catch (IOException ie) {
117                        // スルーしてfalseを返す
118                }
119                return flgRtn;
120        }
121
122        /**
123         * 保存先のローカル判定
124         * 
125         * 判定結果を返します。
126         * trueの場合は、ローカル保存。
127         * falseの場合は、クラウドストレージに保存です。
128         * 
129         * @return ローカルフラグ
130         */
131        public boolean isLocal() {
132                return true;
133        }
134        
135        /**
136         * カノニカルファイル取得
137         * 
138         * カノニカルファイル情報を取得します。
139         * 
140         * @throws IOException ファイル関連エラー情報
141         * @return カノニカルファイル情報
142         */
143        public FileOperation getCanonicalFile() throws IOException {
144        String canonPath = getCanonicalPath();
145        return new FileOperation(canonPath);
146    }
147        
148        /**
149         * バケット名取得
150         * 
151         * @return バケット名
152         */
153        public String getBucket() {
154                return this.mybucket;
155        }
156        
157        /**
158         * プラグイン名取得
159         * 
160         * @return プラグイン名
161         */
162        public String getPlugin() {
163                return this.myplugin;
164        }
165        
166        /**
167         * プラグイン名のセット
168         * 
169         * @param plugin プラグイン名
170         */
171        protected void setPlugin( String plugin ) {
172                myplugin = plugin;
173        }
174        
175        
176//      /** テスト用メソッドです。*/
177//      public static void main(String[] args) {
178//              System.out.println("start");
179//
180//              try {
181//                      test01();
182//              }catch(IOException ie) {
183//                      System.out.println(ie);
184//              }
185//
186//              System.out.println("end");
187//      }
188//      
189//      public static void test01() throws IOException{
190//              File file = new FileOperation("test.txt");
191//              File file2 = file.getCanonicalFile();
192//              
193//              System.out.println(file2.getClass());
194//              
195//              FileOperation fo = (FileOperation)file2;
196//              System.out.println(fo.getPath());
197//      }
198//      
199//      public static void writeTest() {
200//              File file = new FileOperation("test.txt");
201//              FileOperation fileOperation = (FileOperation) file;
202////            FileOperation_AWS aws = (FileOperation_AWS)file;
203//              //              file.delete();
204//
205//              try (ByteArrayInputStream bais = new ByteArrayInputStream("テスト".getBytes())) {
206//                      fileOperation.write(bais);
207//              } catch (IOException ie) {
208//                      System.out.println(ie);
209//              }
210//      }
211}