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(final String path) {
038                super(path);
039        }
040        
041        /**
042         * コンストラクタ
043         * 
044         * FileOperationクラスでは、buketは使用しません。
045         * 
046         * @param bucket バケット名
047         * @param path ファイルパス
048         */
049        public FileOperation(final String bucket, final 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(final 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(final 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        @Override
144        public FileOperation getCanonicalFile() throws IOException {
145                final String canonPath = getCanonicalPath();
146                return new FileOperation(canonPath);
147        }
148        
149        /**
150         * バケット名取得。
151         * 
152         * バケット名を取得します。
153         * 
154         * @return バケット名
155         */
156        public String getBucket() {
157                return this.mybucket;
158        }
159        
160        /**
161         * プラグイン名取得。
162         * 
163         * プラグイン名を取得します。
164         * 
165         * @return プラグイン名
166         */
167        public String getPlugin() {
168                return this.myplugin;
169        }
170        
171        /**
172         * プラグイン名のセット。
173         * 
174         * プラグイン名をセットします。
175         * 
176         * @param plugin プラグイン名
177         */
178        protected void setPlugin( final String plugin ) {
179                myplugin = plugin;
180        }
181        
182        
183//      /** テスト用メソッドです。*/
184//      public static void main(String[] args) {
185//              System.out.println("start");
186//
187//              try {
188//                      test01();
189//              }catch(IOException ie) {
190//                      System.out.println(ie);
191//              }
192//
193//              System.out.println("end");
194//      }
195//      
196//      public static void test01() throws IOException{
197//              File file = new FileOperation("test.txt");
198//              File file2 = file.getCanonicalFile();
199//              
200//              System.out.println(file2.getClass());
201//              
202//              FileOperation fo = (FileOperation)file2;
203//              System.out.println(fo.getPath());
204//      }
205//      
206//      public static void writeTest() {
207//              File file = new FileOperation("test.txt");
208//              FileOperation fileOperation = (FileOperation) file;
209////            FileOperation_AWS aws = (FileOperation_AWS)file;
210//              //              file.delete();
211//
212//              try (ByteArrayInputStream bais = new ByteArrayInputStream("テスト".getBytes())) {
213//                      fileOperation.write(bais);
214//              } catch (IOException ie) {
215//                      System.out.println(ie);
216//              }
217//      }
218}