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
018import java.io.File;
019import java.io.InputStream;
020import java.io.OutputStream;
021import java.io.BufferedOutputStream;
022import java.io.FileOutputStream;
023import java.io.IOException;
024import jakarta.servlet.ServletInputStream;
025import 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) クラウド対応
028import 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) クラウドストレージ対応を追加。引数にstorageType,bucketNameを追加。
137         *
138         * @param       fileOrDirectory 出力先ファイル名/ディレクトリ名
139         * @param  storageType ストレージタイプ
140         * @param       bucketName      バケット名
141         *
142         * @return      ストリームに書き出したバイト数
143         * @throws  IOException 入出力エラーが発生したとき
144         */
145//      public long writeTo( final File fileOrDirectory ) throws IOException {
146        public long writeTo( final FileOperation fileOrDirectory, final String storageType, final String bucketName ) throws IOException {
147                long written = 0;
148
149//              OutputStream fileOut = null;
150                try {
151                        // Only do something if this part contains a file
152                        if( filename != null ) {
153                                // Check if user supplied directory
154//                              File file;
155                                FileOperation file;
156//                              if( fileOrDirectory.isDirectory() ) {
157                                if(fileOrDirectory.isDirectory() || !fileOrDirectory.isFile()) {
158                                        // Write it to that dir the user supplied,
159                                        // with the filename it arrived with
160//                                      file = new File(fileOrDirectory, filename);
161                                        file = HybsFileOperationFactory.create(storageType, bucketName, fileOrDirectory, filename);
162                                }
163                                else {
164                                        // Write it to the file the user supplied,
165                                        // ignoring the filename it arrived with
166                                        file = fileOrDirectory;
167                                }
168//                              fileOut = new BufferedOutputStream(new FileOutputStream(file));
169//                              written = write(fileOut);
170                                file.write(partInput);
171                                written = file.length();
172                        }
173                }
174                finally {
175//                      Closer.ioClose( fileOut );              // 4.0.0 (2006/01/31) close 処理時の IOException を無視
176                        Closer.ioClose( partInput );    // V5 には入っていない…入れて大丈夫か?
177                }
178                return written;
179        }
180
181        /**
182         * 指定のストリームに書き出します。
183         *
184         * @og.rev 6.3.9.1 (2015/11/27) 修飾子を、なし → private に変更。
185         *
186         * @param       outStrm OutputStreamオブジェクト
187         *
188         * @return      ストリームに書き出したバイト数
189         * @throws  IOException 入出力エラーが発生したとき
190         */
191        private long write( final OutputStream outStrm ) throws IOException {
192                // decode macbinary if this was sent
193                long size=0;
194                int read;
195                final byte[] buf = new byte[8 * 1024];
196                while((read = partInput.read(buf)) != -1) {
197                        outStrm.write(buf, 0, read);
198                        size += read;
199                }
200                return size;
201        }
202
203        /**
204         * ファイルかどうか。
205         *
206         * @return      (常に true)
207         */
208        @Override
209        public boolean isFile() {
210                return true;
211        }
212}