001/**
002 * Copyright (c) 2015-2022, Michael Yang 杨福海 (fuhai999@gmail.com).
003 * <p>
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 * <p>
008 * http://www.apache.org/licenses/LICENSE-2.0
009 * <p>
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, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package io.jboot.web.attachment;
017
018import com.jfinal.ext.kit.DateKit;
019import com.jfinal.kit.LogKit;
020import io.jboot.utils.FileUtil;
021import io.jboot.utils.StrUtil;
022
023import java.io.File;
024import java.io.FileOutputStream;
025import java.io.IOException;
026import java.io.InputStream;
027import java.util.Date;
028
029/**
030 * @author michael yang (fuhai999@gmail.com)
031 */
032public class LocalAttachmentContainer implements AttachmentContainer {
033
034    private String rootPath;
035    private String targetPrefix;
036
037    public LocalAttachmentContainer() {
038        LocalAttachmentContainerConfig config = LocalAttachmentContainerConfig.getInstance();
039        this.rootPath = config.getRootPath();
040        this.targetPrefix = config.getTargetPrefix();
041    }
042
043
044    /**
045     * @param rootPath
046     * @param targetPrefix 不能以 / 开头
047     */
048    public LocalAttachmentContainer(String rootPath, String targetPrefix) {
049        this.rootPath = rootPath;
050        this.targetPrefix = targetPrefix;
051    }
052
053
054    @Override
055    public String saveFile(File file) {
056        File newfile = creatNewFile(FileUtil.getSuffix(file.getName()));
057
058        if (!newfile.getParentFile().exists()) {
059            newfile.getParentFile().mkdirs();
060        }
061
062        try {
063            org.apache.commons.io.FileUtils.moveFile(file, newfile);
064            newfile.setReadable(true, false);
065        } catch (IOException e) {
066            LogKit.error(e.toString(), e);
067        }
068
069        String attachmentRoot = getRootPath();
070        return FileUtil.removePrefix(newfile.getAbsolutePath(), attachmentRoot);
071    }
072
073
074    @Override
075    public String saveFile(File file, String toRelativePath) {
076        File toFile = new File(getRootPath(), toRelativePath);
077        try {
078
079            //相同的文件,不需要做任何处理
080            if (toFile.equals(file)) {
081                return toRelativePath;
082            }
083
084            if (!toFile.getParentFile().exists()) {
085                toFile.getParentFile().mkdirs();
086            }
087
088            org.apache.commons.io.FileUtils.moveFile(file, toFile);
089            toFile.setReadable(true, false);
090
091        } catch (IOException e) {
092            LogKit.error(e.toString(), e);
093        }
094
095        return toRelativePath;
096
097
098    }
099
100    @Override
101    public String saveFile(InputStream inputStream, String toRelativePath) {
102
103        File toFile = new File(getRootPath(), toRelativePath);
104
105        if (toFile.exists()) {
106            toFile.delete();
107        }
108
109        if (!toFile.getParentFile().exists()) {
110            toFile.getParentFile().mkdirs();
111        }
112
113        FileOutputStream fOutStream = null;
114        try {
115            fOutStream = new FileOutputStream(toFile);
116            byte[] buffer = new byte[1024];
117            int len;
118            while ((len = inputStream.read(buffer)) > -1) {
119                fOutStream.write(buffer, 0, len);
120            }
121        } catch (IOException e) {
122            LogKit.error(e.toString(), e);
123        } finally {
124            FileUtil.close(fOutStream, inputStream);
125        }
126        return toRelativePath;
127    }
128
129
130    @Override
131    public boolean deleteFile(String relativePath) {
132        File file = getFile(relativePath);
133        return file.exists() && file.delete();
134    }
135
136
137    public File creatNewFile(String suffix) {
138        String rootPath = getRootPath();
139
140        StringBuilder newFileName = new StringBuilder(rootPath).append(targetPrefix)
141                .append(File.separator).append(DateKit.toStr(new Date(), "yyyyMMdd"))
142                .append(File.separator).append(StrUtil.uuid())
143                .append(suffix);
144
145        return new File(newFileName.toString());
146    }
147
148
149    @Override
150    public File getFile(String relativePath) {
151        return new File(getRootPath(), relativePath);
152    }
153
154
155    @Override
156    public String getRelativePath(File file) {
157        String rootPath = getRootPath();
158        String filePath = file.getAbsolutePath();
159        return filePath.startsWith(rootPath)
160                ? filePath.substring(rootPath.length())
161                : filePath;
162    }
163
164
165    public String getRootPath() {
166        return rootPath;
167    }
168
169    public void setRootPath(String rootPath) {
170        this.rootPath = rootPath;
171    }
172
173    public String getTargetPrefix() {
174        return targetPrefix;
175    }
176
177    public void setTargetPrefix(String targetPrefix) {
178        this.targetPrefix = targetPrefix;
179    }
180
181
182}