You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
2.8 KiB
71 lines
2.8 KiB
6 years ago
|
package com.fr.plugin.file.submit.oss.fun;
|
||
|
|
||
|
import com.aliyun.oss.OSSClient;
|
||
|
import com.fr.cache.Attachment;
|
||
|
import com.fr.general.FArray;
|
||
|
import com.fr.general.GeneralUtils;
|
||
|
import com.fr.script.Calculator;
|
||
|
import com.fr.stable.StringUtils;
|
||
|
import com.fr.stable.xml.FRFile;
|
||
|
|
||
|
import java.io.ByteArrayInputStream;
|
||
|
|
||
|
public class OssUpload {
|
||
|
|
||
|
public static void upload(Calculator calculator, OSSClient ossClient, OssSubmitTargetConfig submitTargetConfig) throws Exception {
|
||
|
OssSubmitTarget[] submitTargets = submitTargetConfig.getSubmitTargets();
|
||
|
for (OssSubmitTarget target : submitTargets) {
|
||
|
OssVariableValue fileObject = target.getFile();
|
||
|
Object file = calculator.evalValue(GeneralUtils.objectToString(fileObject));
|
||
|
|
||
|
|
||
|
uploadFile(ossClient, target.getBucket(), file,
|
||
|
target.getDirectory() == null ? null : target.getDirectory().getResult(calculator),
|
||
|
target.getName() == null ? null : target.getName().getResult(calculator));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private static void uploadFile(OSSClient ossClient, String bucket, Object file, String directory, String newFileName) {
|
||
|
if (file instanceof FRFile) {
|
||
|
ossClient.putObject(bucket, directory + "/" + combineName(((FRFile) file).getFileName(), newFileName), new ByteArrayInputStream(((FRFile) file).getBytes()));
|
||
|
} else if (file instanceof FArray && ((FArray) file).length() != 0) {
|
||
|
FArray array = (FArray)file;
|
||
|
for (int i = 0; i < array.length(); i++) {
|
||
|
Object element = array.elementAt(i);
|
||
|
if (!(element instanceof Attachment)) {
|
||
|
if (element instanceof FArray) {
|
||
|
uploadFile(ossClient, bucket, element, directory, newFileName);
|
||
|
}
|
||
|
} else {
|
||
|
ossClient.putObject(bucket, directory + "/" + combineName(((Attachment) element).getFilename(), newFileName), new ByteArrayInputStream(((Attachment) element).getBytes()));
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
* 这里的逻辑是:
|
||
|
* 1、如果没有设置文件名,就用上传的文件的名字;
|
||
|
* 2、如果设置了文件名,且带有文件后缀,就用设置的文件名;
|
||
|
* 3、其他情况都用原文件名。
|
||
|
*/
|
||
|
private static String combineName(String fileName, String myFileName) {
|
||
|
String realFileName;
|
||
|
if (StringUtils.isBlank(fileName)) {
|
||
|
fileName = "unnamed.png";
|
||
|
}
|
||
|
if (StringUtils.isNotEmpty(myFileName)) {
|
||
|
int index = myFileName.lastIndexOf(".");
|
||
|
if (index == -1) {
|
||
|
realFileName = fileName;
|
||
|
} else {
|
||
|
realFileName = myFileName;
|
||
|
}
|
||
|
} else {
|
||
|
realFileName = fileName;
|
||
|
}
|
||
|
return realFileName;
|
||
|
}
|
||
|
|
||
|
}
|