2 changed files with 130 additions and 0 deletions
@ -0,0 +1,102 @@
@@ -0,0 +1,102 @@
|
||||
package apelet.association.plugin.fileLibraryMange; |
||||
|
||||
import apelet.common.core.object.ObjectValue; |
||||
import apelet.common.online.abstractplugin.ExecutePluginParent; |
||||
import apelet.common.online.model.constant.AttributeEnum; |
||||
import apelet.common.orm.impl.Selector; |
||||
import apelet.common.orm.impl.SelectorItem; |
||||
import com.alibaba.fastjson.JSON; |
||||
import com.alibaba.fastjson.JSONArray; |
||||
import com.alibaba.fastjson.JSONObject; |
||||
|
||||
import java.io.File; |
||||
import java.io.FileInputStream; |
||||
|
||||
public class FileLibrayFormPlugin extends ExecutePluginParent { |
||||
@Override |
||||
public void formCreated(String widgetVariableName, ObjectValue objectValue) { |
||||
super.formCreated(widgetVariableName, objectValue); |
||||
Object viewCount = objectValue.get("read_count"); |
||||
if(objectValue.get("classification") == null){ |
||||
this.setWidgetAttribute("classification", AttributeEnum.VALUE_CHANGE,"0"); |
||||
} |
||||
if(viewCount == null){ |
||||
this.setWidgetAttribute("readCount", AttributeEnum.VALUE_CHANGE,"0"); |
||||
} |
||||
else{ |
||||
objectValue.put("read_count",(Integer)viewCount+1); |
||||
Selector selector = new Selector(); |
||||
selector.getList().add(new SelectorItem("read_count")); |
||||
try { |
||||
ormGenDataSourceUtil().update(objectValue.getTableName(), objectValue, selector); |
||||
} catch (Exception e) { |
||||
throw new RuntimeException(e); |
||||
} |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void change(String widgetVariableName, ObjectValue objectValue) { |
||||
super.change(widgetVariableName, objectValue); |
||||
Object fileName = objectValue.get("file_name"); |
||||
if(fileName == null){ |
||||
this.setWidgetAttribute("fileType", AttributeEnum.VALUE_CHANGE,null); |
||||
this.setWidgetAttribute("fileSize", AttributeEnum.VALUE_CHANGE,null); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void uploadSuccess(String widgetVariableName, ObjectValue objectValue) { |
||||
super.uploadSuccess(widgetVariableName, objectValue); |
||||
Object fileName = objectValue.get("file_name"); |
||||
|
||||
if(fileName != null){ |
||||
JSONArray fileJson = JSON.parseArray(fileName.toString()); |
||||
JSONObject item = fileJson.getJSONObject(0); |
||||
String realName = item.getString("fileRealName"); |
||||
String fullFilePath = item.getString("uploadPath")+"/"+item.getString("filename"); |
||||
|
||||
File file = new File(fullFilePath); |
||||
|
||||
if (file.exists() && file.isFile()) { |
||||
// 获取文件大小(字节)
|
||||
long fileSize = file.length(); |
||||
|
||||
// 获取文件类型(根据文件名后缀)
|
||||
String fileType = getFileType(realName); |
||||
this.setWidgetAttribute("fileType", AttributeEnum.VALUE_CHANGE,fileType); |
||||
this.setWidgetAttribute("fileSize", AttributeEnum.VALUE_CHANGE,formatFileSize(fileSize)); |
||||
|
||||
} else { |
||||
System.out.println("文件不存在:" + fullFilePath); |
||||
} |
||||
|
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 格式化文件大小 |
||||
* 小于1MB显示KB,大于等于1MB显示MB,返回带单位字符串 |
||||
*/ |
||||
public static String formatFileSize(long sizeByte) { |
||||
final long ONE_MB = 1048576L; |
||||
final long ONE_KB = 1024L; |
||||
if (sizeByte < ONE_MB) { |
||||
// 转KB保留1位小数
|
||||
double kb = (double) sizeByte / ONE_KB; |
||||
return String.format("%.1f KB", kb); |
||||
} else { |
||||
// 转MB保留1位小数
|
||||
double mb = (double) sizeByte / ONE_MB; |
||||
return String.format("%.1f MB", mb); |
||||
} |
||||
} |
||||
|
||||
// ===================== 工具方法:获取文件类型 =====================
|
||||
private String getFileType(String fileName) { |
||||
if (fileName == null || !fileName.contains(".")) { |
||||
return "unknown"; |
||||
} |
||||
return fileName.substring(fileName.lastIndexOf(".") + 1); |
||||
} |
||||
} |
||||
@ -0,0 +1,28 @@
@@ -0,0 +1,28 @@
|
||||
package apelet.association.plugin.fileLibraryMange; |
||||
|
||||
import apelet.common.online.abstractplugin.ListPlugin; |
||||
import apelet.common.online.dto.OnlineEventPluginExecuteDto; |
||||
import apelet.common.online.dto.OnlineFilterDto; |
||||
import apelet.common.orm.impl.Filter; |
||||
import apelet.common.orm.impl.FilterItem; |
||||
|
||||
import java.util.ArrayList; |
||||
|
||||
public class FileLibrayListPlugin extends ListPlugin { |
||||
@Override |
||||
protected Filter getFilter() { |
||||
OnlineEventPluginExecuteDto filter1 = this.getDto(); |
||||
ArrayList<OnlineFilterDto> filterDtoList = (ArrayList<OnlineFilterDto>) filter1.getListParams().get("filterDtoList"); |
||||
Filter filter = new Filter(); |
||||
if(!filterDtoList.isEmpty()){ |
||||
OnlineFilterDto onlineFilterDto = (OnlineFilterDto) filterDtoList.get(0); |
||||
Object filterFile = onlineFilterDto.getColumnValue(); |
||||
filter.add(new FilterItem("classification","=",filterFile)); |
||||
|
||||
} |
||||
else { |
||||
filter = new Filter(); |
||||
} |
||||
return filter; |
||||
} |
||||
} |
||||
Loading…
Reference in new issue