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 */ 016package org.opengion.hayabusa.io; 017 018import java.io.File; 019import java.util.function.Supplier; // 8.0.0.2 (2021/10/15) 020 021import org.opengion.fukurou.model.FileOperation; 022import org.opengion.fukurou.model.FileOperationFactory; 023import org.opengion.fukurou.util.StringUtil; 024import org.opengion.fukurou.util.FileUtil; // 8.0.0.2 (2021/10/15) 025import org.opengion.hayabusa.common.HybsSystem; 026// import org.opengion.fukurou.system.HybsConst; // 5.10.9.0 (2019/03/01) 027 028/** 029 * クラウドを含むファイル操作クラスの生成 030 * 031 * システムリソース参照のみで、クラウドクラスを生成します。(8.0.0.1 (2021/10/08)で固定) 032 * 引数付きの場合は、直接、org.opengion.fukurou.model.FileOperationFactory をご利用ください。 033 * 034 * 直接fukurouをCallしてもよいのですが、hayabusaからの呼び出しではシステムリソースを参照する必要があるため 035 * ラッパー的にこのクラスを経由してCallする事でシステムリソースが使われるようにしておきます。 036 * (タグ以外からも呼び出されるため、commonTagSupportではなく専用クラスをioパッケージに作成しています) 037 * 038 * ※ plugin → storage に変更。 039 * 040 * ローカルのファイルを扱いたい場合は、storageかbucketにLOCALを指定してください。 041 * 042 * @og.rev 5.10.8.0 (2019/02/01) 新規作成 043 * @og.rev 8.0.0.1 (2021/10/08) 修正対応 044 * @og.group 045 * 046 * @version 5.0 047 * @author Takahashi Masakazu 048 * @since JDK7.0 049 */ 050public final class HybsFileOperationFactory { 051 private static final String STORAGE = HybsSystem.sys("CLOUD_TARGET"); 052 private static final String BUCKET = HybsSystem.sys("CLOUD_BUCKET"); 053 054 /** 055 * コンストラクタはprivate化しておきます。 056 */ 057 private HybsFileOperationFactory(){ 058 // コンストラクタ 059 } 060 061 /** 062 * fukurouのFileOperationFactoryを呼び出してFileOperationを取得します。 063 * storage,bucketを指定しない場合はシステムリソースを利用します。 064 * 065 * @og.rev 8.0.1.0 (2021/10/29) useLocal 属性を追加 066 * 067 * @param useLocal 強制的にローカルファイルを使用する場合、true にセットします。 068 * @param path ファイルパス 069 * @return FileOperationインスタンス 070 */ 071 public static FileOperation create( final boolean useLocal,final String path ) { 072 final String bucket = useLocal ? FileOperation.LOCAL : BUCKET ; 073 074 return FileOperationFactory.newStorageOperation( STORAGE,bucket,path ); 075 } 076 077 /** 078 * ディレクトリとファイル名を指定用です。 079 * 080 * @og.rev 8.0.1.0 (2021/10/29) useLocal 属性を追加 081 * 082 * @param useLocal 強制的にローカルファイルを使用する場合、true にセットします。 083 * @param dir ディレクトリパス 084 * @param fileName ファイル名 085 * @return FileOperationインスタンス 086 */ 087 public static FileOperation create( final boolean useLocal,final String dir, final String fileName ) { 088 final String bucket = useLocal ? FileOperation.LOCAL : BUCKET ; 089 return FileOperationFactory.newStorageOperation( STORAGE,bucket,dir,fileName ); // 連結方法を統一します。 090 } 091 092 /** 093 * FileOperation(ディレクトリ)とファイル名を指定用です。 094 * 095 * @og.rev 8.0.1.0 (2021/10/29) useLocal 属性を追加 096 * 097 * @param useLocal 強制的にローカルファイルを使用する場合、true にセットします。 098 * @param dir ファイル(ディレクトリパス取得) 099 * @param fileName ファイル名 100 * @return FileOperationインスタンス 101 */ 102 public static FileOperation create( final boolean useLocal,final File dir, final String fileName ) { 103 return create( useLocal,new File( dir,fileName ).getPath() ); 104 } 105 106// /** 107// * fukurouのFileOperationFactoryを呼び出してFileOperationを取得します。 108// * storage,bucketを指定しない場合はシステムリソースを利用します。 109// * 110// * @param storage ストレージ(AWS など) 111// * @param bucket バケット名 112// * @param path ファイルパス 113// * @return FileOperationインスタンス 114// */ 115// public static FileOperation create(final String storage, final String bucket, final String path) { 116// return FileOperationFactory.newStorageOperation( 117// StringUtil.nval(storage, STORAGE), StringUtil.nval(bucket, BUCKET), path ); 118// } 119 120// /** 121// * FileOperation(ディレクトリ)とファイル名を指定用です。 122// * 123// * @param storage ストレージ(AWS など) 124// * @param bucket バケット名 125// * @param dir ファイル(ディレクトリパス取得) 126// * @param filename ファイル名 127// * @return FileOperationインスタンス 128// */ 129// public static FileOperation create(final String storage, final String bucket, final File dir, final String filename) { 130// return create(storage, bucket, new File( dir,filename ).getPath() ); 131// } 132 133// /** 134// * ディレクトリとファイル名を指定用です。 135// * 136// * @param storage ストレージ(AWS など) 137// * @param bucket バケット名 138// * @param dir ディレクトリパス 139// * @param filename ファイル名 140// * @return FileOperationインスタンス 141// */ 142// public static FileOperation create(final String storage, final String bucket, final String dir, final String filename) { 143// return create(storage, bucket, new File( dir,filename ).getPath() ); 144// } 145 146 /** 147 * システム定数で、クラウド設定されているかどうか 148 * 149 * システム定数の、CLOUD_TARGET か、CLOUD_BUCKET のどちらかが、 150 * null(ゼロ文字列、タブ、空白のみ)の場合、false です。 151 * それ以外は、true を返しますが、正常にクラウドにアクセス出来る保証はありません。 152 * あくまで、リソース設定がされているかどうかのみで、判定しています。 153 * 154 * @og.rev 8.0.0.1 (2021/10/08) クラウド修正 155 * 156 * @return クラウド設定されていれば true 157 */ 158 public static boolean useCloud() { 159// return ! StringUtil.isNull( STORAGE,BUCKET ); // どれか一つでも null なら、false 160 161 final boolean isLocal = StringUtil.isNull( STORAGE,BUCKET ) 162 || FileOperation.LOCAL.equalsIgnoreCase(STORAGE) 163 || FileOperation.LOCAL.equalsIgnoreCase(BUCKET) ; 164 165 return ! isLocal ; 166 } 167 168 /** 169 * ローカルファイルをクラウドに移動<del>後、ローカルファイルを削除</del>します。 170 * 171 * クラウド設定されている場合、指定のサプライヤを実行してローカルファイルを取得し、 172 * クラウドにコピー後、ローカルファイルを削除します。 173 * クラウド設定されていなければ、何もしません。 174 * 175 * @og.rev 8.0.0.2 (2021/10/15) ローカルファイルとクラウドファイル間の移動 176 * @og.rev 8.0.1.0 (2021/10/29) useLocal 属性を追加、ファイル削除は行わない。 177 * 178 * @param useLocal 強制的にローカルファイルを使用する場合、true にセットします。 179 * @param supp ローカルファイルを生成するサプライヤ 180 */ 181 public static void local2cloud( final boolean useLocal,final Supplier<File> supp ) { 182 if( !useLocal && useCloud() ) { 183 final File localFile = supp.get(); 184 final FileOperation cloudFile = create( false,localFile.getPath() ); 185 FileUtil.copy( localFile, cloudFile ); 186 // localFile.delete(); 187 } 188 } 189 190 /** 191 * クラウドファイルをローカルに移動<del>後、クラウドファイルを</del>削除します。 192 * 193 * クラウド設定されている場合、指定のサプライヤを実行してローカルファイルを取得し、 194 * まず、ローカルファイルを削除後、ローカルファイルの親フォルダを作成して、 195 * クラウドファイルをローカルに移動後、クラウドファイルを削除します。 196 * クラウド設定されていなければ、何もしません。 197 * 198 * @og.rev 8.0.0.2 (2021/10/15) ローカルファイルとクラウドファイル間の移動 199 * @og.rev 8.0.1.0 (2021/10/29) useLocal 属性を追加、ファイル削除は行わない。 200 * 201 * @param useLocal 強制的にローカルファイルを使用する場合、true にセットします。 202 * @param supp ローカルファイルを生成するサプライヤ 203 */ 204 public static void cloud2local( final boolean useLocal,final Supplier<File> supp ) { 205 if( !useLocal && useCloud() ) { 206 final File localFile = supp.get(); 207 final FileOperation cloudFile = create( false,localFile.getPath() ); 208 209 localFile.delete(); 210 final File localParent = localFile.getParentFile(); 211 if( localParent != null ) { localParent.mkdirs(); } 212 213 FileUtil.copy( cloudFile, localFile ); 214 // cloudFile.delete(); 215 } 216 } 217 218 /** 219 * #create(String,String,String)を呼び出してFileOperationを取得します。 220 * storage,bucketを指定しない場合はシステムリソースを利用します。 221 * 222 * 引数をディレクトリとして処理します。 223 * ローカルフォルダの場合、ディレクトリの作成、ディレクトリかどうかのチェック、書き込みチェックを行います。 224 * 225 * @og.rev 8.0.0.2 (2021/10/15) ローカルファイルとクラウドファイル間の移動 226 * 227// * @param storage ストレージ(AWS など) 228// * @param bucket バケット名 229 * @param useLocal 強制的にローカルファイルを使用する場合、true にセットします。 230 * @param dir ディレクトリパス 231 * @return FileOperationインスタンス 232 * @throws IllegalArgumentException 引数のディレクトリが作成できない、ディレクトリでない、書き込めない場合 233 * @see #create(boolean,String) 234 */ 235// public static FileOperation createDir(final String storage, final String bucket, final String dir) { 236 public static FileOperation createDir(final boolean useLocal, final String dir) { 237// final FileOperation cloudDir = create( storage,bucket,dir ); 238 final FileOperation cloudDir = create( useLocal,dir ); 239 240// if( !useCloud() ) { 241 if( useLocal || !useCloud() ) { 242 // セーブディレクトリ 作成 243 if( ! cloudDir.exists() && ! cloudDir.mkdirs() ) { 244 throw new IllegalArgumentException( "Not make directory: " + dir ); 245 } 246 247 // Check saveDirectory is truly a directory 248 if(!cloudDir.isDirectory()) { 249 throw new IllegalArgumentException("Not a directory: " + dir); 250 } 251 252 // Check saveDirectory is writable 253 if(!cloudDir.canWrite()) { 254 throw new IllegalArgumentException("Not writable: " + dir); 255 } 256 } 257 258 return cloudDir ; 259 } 260}