-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileFunctions.ts
More file actions
253 lines (226 loc) · 8.81 KB
/
FileFunctions.ts
File metadata and controls
253 lines (226 loc) · 8.81 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Moepictures - A cute and moe anime image board ❤ *
* Copyright © 2026 Moebytes <moebytes.com> *
* Licensed under CC BY-NC 4.0. See license.txt for details. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
import path from "path"
import functions from "./Functions"
import fileType from "magic-bytes.js"
import {isLive2DZip as verifyLive2DZip} from "live2d-renderer"
import JSZip from "jszip"
const imageExtensions = [".jpg", ".jpeg", ".png", ".webp", ".avif"]
const animationExtensions = [".gif", ".webp", ".apng", ".png", ".zip"]
const videoExtensions = [".mp4", ".webm", ".mov", ".mkv"]
const audioExtensions = [".mp3", ".wav", ".ogg", ".flac", ".aac"]
const modelExtensions = [".glb", ".gltf", ".fbx", ".vrm", ".obj"]
export default class FileFunctions {
public static isImage = (file?: string) => {
if (!file) return false
file = file.replace(/\?.*$/, "")
if (file?.startsWith("blob:")) {
const ext = file.split("#")?.[1] || ""
return functions.util.arrayIncludes(ext, imageExtensions)
}
if (file.startsWith("data:image")) {
return true
}
const ext = file.startsWith(".") ? file : path.extname(file)
return functions.util.arrayIncludes(ext, imageExtensions)
}
public static isAnimation = (file?: string) => {
if (!file) return false
file = file.replace(/\?.*$/, "")
if (file?.startsWith("blob:")) {
const ext = file.split("#")?.[1] || ""
return functions.util.arrayIncludes(ext, animationExtensions)
}
if (file.startsWith("data:image")) {
return true
}
const ext = file.startsWith(".") ? file : path.extname(file)
return functions.util.arrayIncludes(ext, animationExtensions)
}
public static isAudio = (file?: string) => {
if (!file) return false
file = file.replace(/\?.*$/, "")
if (file?.startsWith("blob:")) {
const ext = file.split("#")?.[1] || ""
return functions.util.arrayIncludes(ext, audioExtensions)
}
const ext = file.startsWith(".") ? file : path.extname(file)
return functions.util.arrayIncludes(ext, audioExtensions)
}
public static isModel = (file?: string) => {
if (!file) return false
file = file.replace(/\?.*$/, "")
if (file?.startsWith("blob:")) {
const ext = file.split("#")?.[1] || ""
return functions.util.arrayIncludes(ext, modelExtensions)
}
const ext = file.startsWith(".") ? file : path.extname(file)
return functions.util.arrayIncludes(ext, modelExtensions)
}
public static isZip = (file?: string) => {
if (!file) return false
file = file.replace(/\?.*$/, "")
if (file?.startsWith("blob:")) {
const ext = file.split("#")?.[1] || ""
return ext === ".zip"
}
const ext = file.startsWith(".") ? file : path.extname(file)
return ext === ".zip"
}
public static isGIF = (file?: string) => {
if (!file) return false
file = file.replace(/\?.*$/, "")
if (file?.startsWith("blob:")) {
const ext = file.split("#")?.[1] || ""
return ext === ".gif"
}
if (file?.startsWith("data:image/gif")) {
return true
}
const ext = file.startsWith(".") ? file : path.extname(file)
return ext === ".gif"
}
public static isWebP = (file?: string) => {
if (!file) return false
file = file.replace(/\?.*$/, "")
if (file?.startsWith("blob:")) {
const ext = file.split("#")?.[1] || ""
return ext === ".webp"
}
if (file?.startsWith("data:image/webp")) {
return true
}
const ext = file.startsWith(".") ? file : path.extname(file)
return ext === ".webp"
}
public static isPNG = (file?: string) => {
if (!file) return false
file = file.replace(/\?.*$/, "")
if (file?.startsWith("blob:")) {
const ext = file.split("#")?.[1] || ""
return ext === ".png" || ext === ".apng"
}
if (file?.startsWith("data:image/png")) {
return true
}
const ext = file.startsWith(".") ? file : path.extname(file)
return ext === ".png" || ext === ".apng"
}
public static isGLTF = (file?: string) => {
if (!file) return false
file = file.replace(/\?.*$/, "")
if (file?.startsWith("blob:")) {
const ext = file.split("#")?.[1] || ""
return ext === ".glb"
}
const ext = file.startsWith(".") ? file : path.extname(file)
return ext === ".glb"
}
public static isOBJ = (file?: string) => {
if (!file) return false
file = file.replace(/\?.*$/, "")
if (file?.startsWith("blob:")) {
const ext = file.split("#")?.[1] || ""
return ext === ".obj"
}
const ext = file.startsWith(".") ? file : path.extname(file)
return ext === ".obj"
}
public static isFBX = (file?: string) => {
if (!file) return false
file = file.replace(/\?.*$/, "")
if (file?.startsWith("blob:")) {
const ext = file.split("#")?.[1] || ""
return ext === ".fbx"
}
const ext = file.startsWith(".") ? file : path.extname(file)
return ext === ".fbx"
}
public static isVRM = (file?: string) => {
if (!file) return false
file = file.replace(/\?.*$/, "")
if (file?.startsWith("blob:")) {
const ext = file.split("#")?.[1] || ""
return ext === ".vrm"
}
const ext = file.startsWith(".") ? file : path.extname(file)
return ext === ".vrm"
}
public static isAnimatedWebp = (buffer: ArrayBuffer) => {
let str = ""
const byteArray = new Uint8Array(Buffer.from(buffer))
for (let i = 0; i < byteArray.length; i++) {
str += String.fromCharCode(byteArray[i])
}
return str.indexOf("ANMF") !== -1
}
public static isAnimatedPng = (buffer: ArrayBuffer) => {
let str = ""
const byteArray = new Uint8Array(Buffer.from(buffer))
for (let i = 0; i < byteArray.length; i++) {
str += String.fromCharCode(byteArray[i])
}
return str.indexOf("acTL") !== -1
}
public static isVideo = (file?: string) => {
if (!file) return false
file = file.replace(/\?.*$/, "")
if (file?.startsWith("blob:")) {
const ext = file.split("#")?.[1] || ""
return functions.util.arrayIncludes(ext, videoExtensions)
}
const ext = file.startsWith(".") ? file : path.extname(file)
return functions.util.arrayIncludes(ext, videoExtensions)
}
public static isMP4 = (file?: string) => {
if (!file) return false
file = file.replace(/\?.*$/, "")
if (file?.startsWith("blob:")) {
const ext = file.split("#")?.[1] || ""
return ext === ".mp4"
}
const ext = file.startsWith(".") ? file : path.extname(file)
return ext === ".mp4"
}
public static isWebM = (file?: string) => {
if (!file) return false
file = file.replace(/\?.*$/, "")
if (file?.startsWith("blob:")) {
const ext = file.split("#")?.[1] || ""
return ext === ".webm"
}
const ext = file.startsWith(".") ? file : path.extname(file)
return ext === ".webm"
}
public static isLive2DZip = async (buffer: ArrayBuffer) => {
return verifyLive2DZip(buffer)
}
public static isLive2D = async (file?: string) => {
if (!file) return false
const buffer = await functions.http.getBuffer(file)
return this.isLive2DZip(buffer)
}
public static isUgoiraZip = async (buffer: ArrayBuffer) => {
let isZip = false
const result = fileType(new Uint8Array(buffer))?.[0] || {mime: ""}
if (result.mime === "application/zip") isZip = true
if (!isZip) return false
const zip = await JSZip.loadAsync(buffer)
let hasImage = false
let hasAnimation = false
for (const [relativePath, file] of Object.entries(zip.files)) {
if (relativePath.startsWith("__MACOSX") || file.dir) continue
if (relativePath.endsWith('animation.json')) hasAnimation = true
if (relativePath.match(/\.(png|jpg|webp|avif)$/)) hasImage = true
}
return hasImage && hasAnimation
}
public static isUgoira = async (file?: string) => {
if (!file) return false
const buffer = await functions.http.getBuffer(file)
return this.isUgoiraZip(buffer)
}
}