-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilesHelper.ts
More file actions
172 lines (159 loc) · 4.45 KB
/
Copy pathFilesHelper.ts
File metadata and controls
172 lines (159 loc) · 4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import { getFilePickerBuilder } from "@nextcloud/dialogs";
import { FileType } from "@nextcloud/files";
import { basename, dirname } from "path";
import SvgHelper from "@/util/SvgHelper";
const FilesHelper = {
isSwarmNode: (nodes) =>
getMainNode(nodes).attributes["ethswarm-node"] !== undefined,
isDialogCancelError: (error) => isDialogCancelError(error),
getSwarmRef: (nodes) => getSwarmRef(nodes),
hasSwarmRef: (nodes) => getSwarmRef(nodes) !== undefined,
canUnshareOnly: (nodes) => {
return nodes.every(
(node) =>
node.attributes["is-mount-root"] === true &&
node.attributes["mount-type"] === "shared",
);
},
canDisconnectOnly: (nodes) => {
return nodes.every(
(node) =>
node.attributes["is-mount-root"] === true &&
node.attributes["mount-type"] === "external",
);
},
isMixedUnshareAndDelete: (nodes) => {
if (nodes.length === 1) {
return false;
}
const hasSharedItems = nodes.some((node) =>
FilesHelper.canUnshareOnly([node]),
);
const hasDeleteItems = nodes.some(
(node) => !FilesHelper.canUnshareOnly([node]),
);
return hasSharedItems && hasDeleteItems;
},
isAllFiles: (nodes) => {
return !nodes.some((node) => node.type !== FileType.File);
},
isAllFolders: (nodes) => {
return !nodes.some((node) => node.type !== FileType.Folder);
},
getStoragePath: (nodes) => getStoragePath(nodes),
getPathParts: (nodes) => getPathParts(nodes),
isFolder: (nodes) => getMainNode(nodes).type === FileType.Folder,
isRoot: (node) => getPathParts(node).length === 1,
isRootLevel: (node) => getPathParts(node).length === 2,
isArchive: (node) => isArchive(node),
isArchiveFolder: (node) =>
getPathParts(node).length === 2 && isArchive(node),
locationPicker: (node, action, logo) =>
getFilePickerBuilder(`Select ${action} Location`)
.setButtonFactory((_selection, path) => {
return FilesHelper.getStoragePath(path) === ""
? []
: [
{
label: basename(path)
? t(
"files",
"{action} to {path}",
{ path: basename(path), action },
undefined,
{
escape: false,
sanitize: false,
},
)
: t("files", action),
type: "primary",
icon: SvgHelper.convert(logo),
callback: () => undefined,
},
];
})
.allowDirectories(true)
.setFilter((n) => {
const isNotArchiveFolder = !FilesHelper.isArchiveFolder(n);
console.log("node:" + FilesHelper.getStoragePath(n));
console.log("file:" + FilesHelper.getStoragePath(node));
const isSameStorage =
FilesHelper.getStoragePath(n) ===
FilesHelper.getStoragePath(node);
return isNotArchiveFolder && isSameStorage;
})
.setMimeTypeFilter([])
.setMultiSelect(false)
.disableNavigation()
.startAt(
dirname(node.path).substring(
0,
dirname(node.path).lastIndexOf("/"),
),
)
.build()
.pick()
.catch((error) => {
if (isDialogCancelError(error)) {
return null;
}
throw error;
}),
downloadFile: (blob, filename) => {
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = filename;
a.click();
URL.revokeObjectURL(url);
},
};
function getSwarmRef(nodes) {
return getMainNode(nodes).attributes["ethswarm-fileref"];
}
function getMainNode(nodes) {
if (Array.isArray(nodes)) {
return nodes[0];
}
return nodes;
}
function getRelativePath(nodes) {
if (typeof nodes === "string") {
return nodes;
}
const node = getMainNode(nodes);
const fullPath = node.attributes.filename;
const root = node.root;
return fullPath.replace(root, "");
}
function getPathParts(path) {
return getRelativePath(path)
.split("/")
.filter((part) => part !== "");
}
function getStoragePath(path) {
const parts = getPathParts(path);
return parts.length > 0 ? parts[0] : "";
}
function isArchive(nodes) {
return getPathParts(nodes)[1] === "Archive - HejBit";
}
function isDialogCancelError(error) {
if (error === null || typeof error === "undefined") {
return true;
}
if (typeof error === "string") {
return /cancel|abort|dismiss|closed|no nodes selected/i.test(error);
}
if (typeof error === "object") {
const message = "message" in error ? String(error.message ?? "") : "";
const name = "name" in error ? String(error.name ?? "") : "";
return (
/abort|cancel|dismiss|closed/i.test(name) ||
/cancel|abort|dismiss|closed|no nodes selected/i.test(message)
);
}
return false;
}
export default FilesHelper;