    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.servlet.multipart;
    017
    018// import java.io.File;
    019import java.io.InputStream;
    020import java.io.OutputStream;
    021// import java.io.BufferedOutputStream;
    022// import java.io.FileOutputStream;
    023import java.io.IOException;
    024import jakarta.servlet.ServletInputStream;
    025// import jakarta.servlet.http.HttpSession;                             // 5.9.25.0 (2017/10/06) ｸﾗｳﾄﾞ対応
    026
    027import org.opengion.fukurou.system.Closer ;                             // 5.9.25.0 (2017/10/06) ｸﾗｳﾄﾞ対応
    028// import org.opengion.hayabusa.common.HybsSystem;                      // 5.9.25.0 (2017/10/06) ｸﾗｳﾄﾞ対応
    029// import org.opengion.hayabusa.io.StorageAPI;                  // 5.9.25.0 (2017/10/06) ｸﾗｳﾄﾞ対応
    030// import org.opengion.hayabusa.io.StorageAPIFactory;   // 5.9.25.0 (2017/10/06) ｸﾗｳﾄﾞ対応
    031
    032import org.opengion.fukurou.model.FileOperation;                        // 8.0.0.0 (2021/09/30)
    033import org.opengion.hayabusa.io.HybsFileOperationFactory;       // 8.0.0.0 (2021/09/30)
    034
    035/**
    036 * ﾌｧｲﾙｱｯﾌﾟﾛｰﾄﾞ時のﾏﾙﾁﾊﾟｰﾄ処理のﾌｧｲﾙﾊﾟｰﾄ部品です｡
    037 *
    038 * ﾌｧｲﾙ情報を取り扱います｡
    039 *
    040 * @og.group その他機能
    041 *
    042 * @version  4.0
    043 * @author   Kazuhiko Hasegawa
    044 * @since    JDK5.0,
    045 */
    046public class FilePart extends Part {
    047
    048        private String filename;
    049        private final String filePath;
    050        private final String contentType;
    051        private final PartInputStream partInput;
    052
    053        /**
    054         * ﾌｧｲﾙﾊﾟｰﾄ部品 ｵﾌﾞｼﾞｪｸﾄを構築する､ｺﾝｽﾄﾗｸﾀｰ
    055         *
    056         * @param       name            Part名称
    057         * @param       in                      ServletInputStreamｵﾌﾞｼﾞｪｸﾄ
    058         * @param       boundary        境界文字
    059         * @param       contentType     ｺﾝﾃﾝﾂﾀｲﾌﾟ
    060         * @param       filename        ﾌｧｲﾙ名
    061         * @param       filePath        ﾌｧｲﾙﾊﾟｽ
    062         * @throws IOException 入出力ｴﾗｰが発生したとき
    063         */
    064        FilePart( final String name, final ServletInputStream in, final String boundary,
    065                        final String contentType, final String filename, final String filePath)
    066                                                                                        throws IOException {
    067                super(name);
    068                this.filename = filename;
    069                this.filePath = filePath;
    070                this.contentType = contentType;
    071                partInput = new PartInputStream(in, boundary);
    072        }
    073
    074        /**
    075         * ﾌｧｲﾙ名を取得します｡
    076         *
    077         * @return      ﾌｧｲﾙ名
    078         */
    079        public String getFilename() {
    080                return filename;
    081        }
    082
    083        /**
    084         * ﾌｧｲﾙ名をｾｯﾄします｡
    085         *
    086         * @param  fname ﾌｧｲﾙ名
    087         */
    088        public void setFilename( final String fname ) {
    089                filename = fname ;
    090        }
    091
    092        /**
    093         * ﾌｧｲﾙﾊﾟｽを取得します｡
    094         *
    095         * @return      ﾌｧｲﾙﾊﾟｽ
    096         */
    097        public String getFilePath() {
    098                return filePath;
    099        }
    100
    101        /**
    102         * ｺﾝﾃﾝﾂﾀｲﾌﾟを取得します｡
    103         *
    104         * @return      ｺﾝﾃﾝﾂﾀｲﾌﾟ
    105         */
    106        public String getContentType() {
    107                return contentType;
    108        }
    109
    110        /**
    111         * 入力ｽﾄﾘｰﾑを取得します｡
    112         *
    113         * @return      入力ｽﾄﾘｰﾑ
    114         */
    115        public InputStream getInputStream() {
    116                return partInput;
    117        }
    118
    119//      /**
    120//       * ｸﾗｳﾄﾞｽﾄﾚｰｼﾞへのｱｯﾌﾟﾛｰﾄﾞ｡
    121//       *
    122//       * @og.rev 5.9.25.0 (2017/10/06) 追加
    123//       *
    124//       * @param storage               ｸﾗｳﾄﾞ種別
    125//       * @param directory             ｱｯﾌﾟﾛｰﾄﾞ先ﾃﾞｨﾚｸﾄﾘ
    126//       * @param hsession              ｾｯｼｮﾝ
    127//       */
    128//      public void writeToCloud( final String storage, final String directory, final HttpSession hsession ) {
    129//              final StorageAPI storageApi = StorageAPIFactory.newStorageAPI( storage, HybsSystem.sys("CLOUD_BUCKET"), hsession );
    130//              storageApi.add( partInput, directory, filename, hsession );
    131//      }
    132
    133        /**
    134         * 指定のﾌｧｲﾙに書き出します｡
    135         *
    136         * @og.rev 5.10.9.0 (2019/03/01) ｸﾗｳﾄﾞｽﾄﾚｰｼﾞ対応を追加｡引数にstorage,bucketを追加｡
    137         * @og.rev 8.0.1.0 (2021/10/29) useLocal 属性を追加｡storageType , bucketName 削除
    138         *
    139         * @param       fileOrDirectory 出力先ﾌｧｲﾙ名/ﾃﾞｨﾚｸﾄﾘ名
    140//       * @param       storage         ｸﾗｳﾄﾞﾌﾟﾗｸﾞｲﾝ名(ﾛｰｶﾙﾌｧｲﾙを強制する場合は､LOCAL を指定する)
    141//       * @param       bucket          ﾊﾞｹｯﾄ名(ﾛｰｶﾙﾌｧｲﾙを強制する場合は､LOCAL を指定する)
    142         * @param       useLocal        強制的にﾛｰｶﾙﾌｧｲﾙを使用する場合､true にｾｯﾄします｡
    143         *
    144         * @return      ｽﾄﾘｰﾑに書き出したﾊﾞｲﾄ数
    145         * @throws  IOException 入出力ｴﾗｰが発生したとき
    146         */
    147//      public long writeTo( final File fileOrDirectory ) throws IOException {
    148//      public long writeTo( final FileOperation fileOrDirectory, final String storage, final String bucket ) throws IOException {
    149        public long writeTo( final FileOperation fileOrDirectory, final boolean useLocal ) throws IOException {
    150                long written = 0;
    151
    152//              OutputStream fileOut = null;
    153                try {
    154                        // Only do something if this part contains a file
    155                        if( filename != null ) {
    156                                // Check if user supplied directory
    157//                              File file;
    158                                FileOperation file;
    159//                              if( fileOrDirectory.isDirectory() ) {
    160                                if(fileOrDirectory.isDirectory() || !fileOrDirectory.isFile()) {
    161                                        // Write it to that dir the user supplied,
    162                                        // with the filename it arrived with
    163                                        // 8.0.1.0 (2021/10/29) storageType , bucketName 削除
    164//                                      file = new File(fileOrDirectory, filename);
    165//                                      file = HybsFileOperationFactory.create(storage, bucket, fileOrDirectory, filename);
    166                                        file = HybsFileOperationFactory.create(useLocal, fileOrDirectory, filename);
    167                                }
    168                                else {
    169                                        // Write it to the file the user supplied,
    170                                        // ignoring the filename it arrived with
    171                                        file = fileOrDirectory;
    172                                }
    173//                              fileOut = new BufferedOutputStream(new FileOutputStream(file));
    174//                              written = write(fileOut);
    175                                file.write(partInput);
    176                                written = file.length();
    177                        }
    178                }
    179                finally {
    180//                      Closer.ioClose( fileOut );              // 4.0.0 (2006/01/31) close 処理時の IOException を無視
    181                        Closer.ioClose( partInput );    // V5 には入っていない…入れて大丈夫か？
    182                }
    183                return written;
    184        }
    185
    186        /**
    187         * 指定のｽﾄﾘｰﾑに書き出します｡
    188         *
    189         * @og.rev 6.3.9.1 (2015/11/27) 修飾子を､なし → private に変更｡
    190         *
    191         * @param       outStrm OutputStreamｵﾌﾞｼﾞｪｸﾄ
    192         *
    193         * @return      ｽﾄﾘｰﾑに書き出したﾊﾞｲﾄ数
    194         * @throws  IOException 入出力ｴﾗｰが発生したとき
    195         */
    196        private long write( final OutputStream outStrm ) throws IOException {
    197                // decode macbinary if this was sent
    198                long size=0;
    199                int read;
    200                final byte[] buf = new byte[8 * 1024];
    201                while((read = partInput.read(buf)) != -1) {
    202                        outStrm.write(buf, 0, read);
    203                        size += read;
    204                }
    205                return size;
    206        }
    207
    208        /**
    209         * ﾌｧｲﾙかどうか｡
    210         *
    211         * @return      (常に true)
    212         */
    213        @Override
    214        public boolean isFile() {
    215                return true;
    216        }
    217}