Files
schisandra-cloud-storage-fr…/src/store/modules/file.ts
2024-07-19 18:58:47 +08:00

200 lines
6.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/** @format */
import { action, makeObservable, observable } from "mobx";
import { isHydrated, makePersistable } from "mobx-persist-store";
import { handleLocalforage } from "@/utils/localforage";
export class useFileStore {
filePath: any[] = []; // 文件路径
currentStorage: string = ""; // 当前存储商
currentBucket: string = ""; // 当前存储桶
currentFile: string = ""; // 当前文件
copyFile: string = ""; // 复制的文件地址
pasteFile: string = ""; //粘贴的地址
copyFileName: string = ""; // 复制的文件名
uploadFileStorage: string = ""; // 上传文件的存储商
uploadFileBucket: string = ""; // 上传文件的存储桶
uploadFilePath: string = ""; // 上传文件的路径
constructor() {
makeObservable(this, {
filePath: observable,
currentStorage: observable,
currentFile: observable,
currentBucket: observable,
copyFile: observable,
pasteFile: observable,
copyFileName: observable,
uploadFileStorage: observable,
uploadFileBucket: observable,
uploadFilePath: observable,
setFilePath: action,
getFilePath: action,
clearFilePath: action,
getFilePathSecondLast: action,
getMiddlePath: action,
clearAllFilePath: action,
getFilePathExceptFirst: action,
setCurrentBucket: action,
setCurrentStorage: action,
getCurrentBucket: action,
getCurrentStorage: action,
getCurrentFile: action,
setCurrentFile: action,
isHydrated: action,
setCopyFile: action,
getCopyFile: action,
setPasteFile: action,
getPasteFile: action,
getCopyFileName: action,
setCopyFileName: action,
setUploadFileStorage: action,
getUploadFileStorage: action,
setUploadFileBucket: action,
getUploadFileBucket: action,
setUploadFilePath: action,
getUploadFilePath: action,
});
makePersistable(
this,
{
// 在构造函数内使用 makePersistable
name: "file", // 保存的name用于在storage中的名称标识只要不和storage中其他名称重复就可以
properties: [
"filePath",
"currentStorage",
"currentBucket",
"currentFile",
"copyFile",
"pasteFile",
"copyFileName",
], // 要保存的字段这些字段会被保存在name对应的storage中注意不写在这里面的字段将不会被保存刷新页面也将丢失get字段例外。get数据会在数据返回后再自动计算
storage: handleLocalforage, // 保存的位置可以是localStoragesessionstorage
// removeOnExpiration: true, //如果 expireIn 具有值且已过期,则在调用 getItem 时将自动删除存储中的数据。默认值为 true。
// stringify: false, //如果为 true则数据在传递给 setItem 之前将是 JSON.stringify。默认值为 true。
// expireIn: 2592000000, // 一个以毫秒为单位的值,用于确定 getItem 何时不应检索存储中的数据。默认情况下永不过期。
// debugMode: false, // 如果为 true将为多个 mobx-persist-store 项调用 console.info。默认值为 false。
},
{
// delay: 0, // 允许您设置一个 delay 选项来限制 write 函数的调用次数。默认情况下没有延迟。
// fireImmediately: false, // 确定是应立即保留存储数据,还是等到存储中的属性发生更改。 false 默认情况下。
},
).then(
action(() => {
// persist 完成的回调在这里可以执行一些拿到数据后需要执行的操作如果在页面上要判断是否完成persist使用 isHydrated
// console.log(persistStore)
}),
);
}
// 获取文件路径
getFilePath() {
return this.filePath ? this.filePath : [];
}
// 设置文件路径
setFilePath(path: any) {
this.filePath.push(path);
}
// 删除文件路径最后一个
clearFilePath() {
if (this.filePath.length === 1) {
return;
}
this.filePath.pop();
}
// 清空所有文件路径
clearAllFilePath() {
this.filePath.splice(0);
}
// 获取文件路径倒数第二个
getFilePathSecondLast() {
return this.filePath.slice(0, -1).pop();
}
// 获取文件路径除了第一个
getFilePathExceptFirst() {
if (this.filePath.length === 1) {
return "";
} else {
return this.filePath.slice(1).join("/");
}
}
// 获取文件路径中间路径
getMiddlePath() {
if (this.filePath.length <= 2) {
return " ";
}
return this.filePath.slice(1, this.filePath.length - 1).join("/");
}
isHydrated() {
return isHydrated(this);
}
// 设置当前存储桶
setCurrentBucket(currentBucket: string) {
this.currentBucket = currentBucket;
}
// 设置当前存储商
setCurrentStorage(currentStorage: string) {
this.currentStorage = currentStorage;
}
// 获取当前存储商
getCurrentStorage() {
return this.currentStorage ? this.currentStorage : null;
}
// 获取当前存储桶
getCurrentBucket() {
return this.currentBucket ? this.currentBucket : null;
}
// 获取当前文件
getCurrentFile() {
return this.currentFile ? this.currentFile : null;
}
// 设置当前文件
setCurrentFile(currentFile: string) {
return (this.currentFile = currentFile);
}
//设置复制文件
setCopyFile(copyFile: string) {
this.copyFile = copyFile;
}
// 设置粘贴文件
setPasteFile(pasteFile: string) {
this.pasteFile = pasteFile;
}
// 获取复制文件
getCopyFile() {
return this.copyFile ? this.copyFile : null;
}
// 获取粘贴文件
getPasteFile() {
return this.pasteFile ? this.pasteFile : null;
}
// 设置文件名称
setCopyFileName(copyFileName: string) {
this.copyFileName = copyFileName;
}
// 获取文件名称
getCopyFileName() {
return this.copyFileName ? this.copyFileName : null;
}
setUploadFileStorage(uploadFileStorage: string) {
this.uploadFileStorage = uploadFileStorage;
}
getUploadFileStorage() {
return this.uploadFileStorage ? this.uploadFileStorage : null;
}
setUploadFileBucket(uploadFileBucket: string) {
this.uploadFileBucket = uploadFileBucket;
}
getUploadFileBucket() {
return this.uploadFileBucket ? this.uploadFileBucket : null;
}
setUploadFilePath(uploadFilePath: string) {
this.uploadFilePath = uploadFilePath;
}
getUploadFilePath() {
return this.uploadFilePath ? this.uploadFilePath : null;
}
}