From 700a2eff6089fda3f452d3ef732aff0977dafd7e Mon Sep 17 00:00:00 2001 From: simba Date: Mon, 15 Jun 2020 18:18:02 +0800 Subject: [PATCH] share model --- src/common/model/share/Share.ts | 183 ++++++++++++++++++++ src/common/model/share/ShareExpireOption.ts | 60 +++++++ src/common/model/share/ShareType.ts | 33 ++++ 3 files changed, 276 insertions(+) create mode 100644 src/common/model/share/Share.ts create mode 100644 src/common/model/share/ShareExpireOption.ts create mode 100644 src/common/model/share/ShareType.ts diff --git a/src/common/model/share/Share.ts b/src/common/model/share/Share.ts new file mode 100644 index 0000000..47b1615 --- /dev/null +++ b/src/common/model/share/Share.ts @@ -0,0 +1,183 @@ +import BaseEntity from "../base/BaseEntity"; +import Matter from "../matter/Matter"; +import FileUtil from "../../util/FileUtil"; +import EnvUtil from "../../util/EnvUtil"; +import { ShareType } from "./ShareType"; +import { ShareExpireOption, ShareExpireOptionMap } from "./ShareExpireOption"; +import DateUtil from "../../util/DateUtil"; +import SafeUtil from "../../util/SafeUtil"; + +export default class Share extends BaseEntity { + name: string | null = null; + shareType: ShareType = ShareType.MIX; + userUuid: string | null = null; + username: string | null = null; + downloadTimes: number = 0; + code: string | null = null; + expireInfinity: boolean = false; + expireTime: string | null = null; + + //当前正在查看的文件夹 + dirMatter: Matter = new Matter(); + //当前share对应的matters + matters: Matter[] = []; + + //当前分享正在查看的根目录。前端辅助字段。 + rootUuid: string = Matter.MATTER_ROOT; + + //本地临时字段 + expireOption: ShareExpireOption = ShareExpireOption.MONTH; + + static URL_CREATE = "/api/share/create"; + static URL_BROWSE = "/api/share/browse"; + static URL_DELETE_BATCH = "/api/share/delete/batch"; + static URL_ZIP = "/api/share/zip"; + + constructor(reactComponent?: React.Component) { + super(reactComponent); + } + + assign(obj: any) { + super.assign(obj); + super.assignEntity("expireTime", Date); + super.assignEntity("dirMatter", Matter); + super.assignList("matters", Matter); + } + + getUrlPrefix() { + return "/api/share"; + } + + getFilters() { + return [...super.getFilters()]; + } + + getForm() { + return { + name: this.name, + uuid: this.uuid ? this.uuid : null, + }; + } + + getIcon() { + if (this.shareType === ShareType.MIX) { + return require("../../../assets/image/file/archive.svg"); + } else { + return FileUtil.getIcon( + this.name, + this.shareType === ShareType.DIRECTORY + ); + } + } + + getLink() { + return EnvUtil.currentHost() + "/share/detail/" + this.uuid; + } + + hasExpired() { + if (this.expireInfinity) { + return false; + } else { + if (this.expireTime) { + return new Date(this.expireTime).getTime() < new Date().getTime(); + } else { + return false; + } + } + } + + //获取过期时间 + getExpireTime() { + const delta = ShareExpireOptionMap[this.expireOption].deltaMillisecond; + const now = new Date(); + return new Date(now.getTime() + delta); + } + + //下载zip包 + downloadZip(puuid?: string) { + window.open( + EnvUtil.currentHost() + + Share.URL_ZIP + + "?shareUuid=" + + this.uuid + + "&code=" + + this.code + + "&puuid=" + + puuid + + "&rootUuid=" + + this.rootUuid + ); + } + + //创建一个分享matterUuids要求为数组,expireTime要求为时间对象 + httpCreate( + matterUuids: string, + successCallback?: () => any, + errorCallback?: () => any + ) { + let that = this; + + const form = { + matterUuids: matterUuids.toString(), + expireInfinity: this.expireOption === ShareExpireOption.INFINITY, + expireTime: DateUtil.simpleDateTime(this.getExpireTime()), + }; + + this.httpPost( + Share.URL_CREATE, + form, + function (response: any) { + that.assign(response.data.data); + SafeUtil.safeCallback(successCallback)(response); + }, + errorCallback + ); + } + + httpDeleteBatch( + uuids: string, + successCallback?: () => any, + errorCallback?: () => any + ) { + this.httpPost( + Share.URL_DELETE_BATCH, + { uuids: uuids }, + function (response: any) { + SafeUtil.safeCallback(successCallback)(response); + }, + errorCallback + ); + } + + httpBrowse( + puuid: string, + rootUuid: string, + successCallback?: () => any, + errorCallback?: () => any + ) { + let that = this; + + const form = { + puuid, + rootUuid, + shareUuid: this.uuid, + code: this.code, + }; + + this.detailLoading = true; + this.httpPost( + Share.URL_BROWSE, + form, + function (response: any) { + that.assign(response.data.data); + + that.detailLoading = false; + SafeUtil.safeCallback(successCallback)(response); + }, + function (error: any) { + that.detailLoading = false; + SafeUtil.safeCallback(errorCallback)(error); + } + ); + } +} diff --git a/src/common/model/share/ShareExpireOption.ts b/src/common/model/share/ShareExpireOption.ts new file mode 100644 index 0000000..b3256da --- /dev/null +++ b/src/common/model/share/ShareExpireOption.ts @@ -0,0 +1,60 @@ +import SelectionOption from "../base/option/SelectionOption"; + +enum ShareExpireOption { + HOUR = "HOUR", + DAY = "DAY", + WEEK = "WEEK", + MONTH = "MONTH", + YEAR = "YEAR", + INFINITY = "INFINITY", +} + +interface ShareExpireSelection extends SelectionOption { + deltaMillisecond: number; +} + +const ShareExpireOptions: ShareExpireOption[] = Object.keys( + ShareExpireOption +).map((k) => k as ShareExpireOption); + +const ShareExpireOptionMap: { + [key in keyof typeof ShareExpireOption]: ShareExpireSelection; +} = { + HOUR: { + name: "share.hour", + value: "HOUR", + deltaMillisecond: 60 * 60 * 1000, + }, + DAY: { + name: "share.day", + value: "DAY", + deltaMillisecond: 24 * 60 * 60 * 1000, + }, + WEEK: { + name: "share.week", + value: "WEEK", + deltaMillisecond: 7 * 24 * 60 * 60 * 1000, + }, + MONTH: { + name: "share.month", + value: "MONTH", + deltaMillisecond: 30 * 24 * 60 * 60 * 1000, + }, + YEAR: { + name: "share.year", + value: "YEAR", + deltaMillisecond: 365 * 24 * 60 * 60 * 1000, + }, + INFINITY: { + name: "share.infinity", + value: "INFINITY", + deltaMillisecond: 0, + }, +}; + +let ShareExpireOptionList: ShareExpireSelection[] = []; +ShareExpireOptions.forEach((type: ShareExpireOption) => { + ShareExpireOptionList.push(ShareExpireOptionMap[type]); +}); + +export { ShareExpireOption, ShareExpireOptionMap, ShareExpireOptionList }; diff --git a/src/common/model/share/ShareType.ts b/src/common/model/share/ShareType.ts new file mode 100644 index 0000000..46882c1 --- /dev/null +++ b/src/common/model/share/ShareType.ts @@ -0,0 +1,33 @@ +import SelectionOption from "../base/option/SelectionOption"; + +enum ShareType { + FILE = "FILE", + DIRECTORY = "DIRECTORY", + MIX = "MIX", +} + +const ShareTypes: ShareType[] = Object.keys(ShareType).map( + (k) => k as ShareType +); + +const ShareTypeMap: { [key in keyof typeof ShareType]: SelectionOption } = { + FILE: { + name: "文件", + value: "FILE", + }, + DIRECTORY: { + name: "文件夹", + value: "DIRECTORY", + }, + MIX: { + name: "混合", + value: "MIX", + }, +}; + +let ShareTypeList: SelectionOption[] = []; +ShareTypes.forEach((type: ShareType) => { + ShareTypeList.push(ShareTypeMap[type]); +}); + +export { ShareType, ShareTypeMap, ShareTypeList };