forked from ImTheSquid/SplitLargeFiles
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSplitLargeFiles.plugin.js
845 lines (810 loc) · 38.2 KB
/
SplitLargeFiles.plugin.js
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
/**
* @name SplitLargeFiles
* @description Splits files larger than the upload limit into smaller chunks that can be redownloaded into a full file later.
* @version 1.9.7
* @author ImTheSquid & Riolubruh
* @authorId 262055523896131584
* @website https://github.com/riolubruh/SplitLargeFiles
* @source https://github.com/riolubruh/SplitLargeFiles
* @updateUrl https://raw.githubusercontent.com/riolubruh/SplitLargeFiles/main/SplitLargeFiles.plugin.js
*/
/*@cc_on
@if (@_jscript)
// Offer to self-install for clueless users that try to run this directly.
var shell = WScript.CreateObject("WScript.Shell");
var fs = new ActiveXObject("Scripting.FileSystemObject");
var pathPlugins = shell.ExpandEnvironmentStrings("%APPDATA%\\BetterDiscord\\plugins");
var pathSelf = WScript.ScriptFullName;
// Put the user at ease by addressing them in the first person
shell.Popup("It looks like you've mistakenly tried to run me directly. \n(Don't do that!)", 0, "I'm a plugin for BetterDiscord", 0x30);
if (fs.GetParentFolderName(pathSelf) === fs.GetAbsolutePathName(pathPlugins)) {
shell.Popup("I'm in the correct folder already.", 0, "I'm already installed", 0x40);
} else if (!fs.FolderExists(pathPlugins)) {
shell.Popup("I can't find the BetterDiscord plugins folder.\nAre you sure it's even installed?", 0, "Can't install myself", 0x10);
} else if (shell.Popup("Should I copy myself to BetterDiscord's plugins folder for you?", 0, "Do you need some help?", 0x34) === 6) {
fs.CopyFile(pathSelf, fs.BuildPath(pathPlugins, fs.GetFileName(pathSelf)), true);
// Show the user where to put plugins in the future
shell.Exec("explorer " + pathPlugins);
shell.Popup("I'm installed!", 0, "Successfully installed", 0x40);
}
WScript.Quit();
@else@*/
const config = {
info: {
name: "SplitLargeFiles",
authors: [
{
name: "ImTheSquid",
discord_id: "262055523896131584",
github_username: "ImTheSquid",
twitter_username: "ImTheSquid11"
}, {
name: "Riolubruh",
discord_id: "359063827091816448",
github_username: "riolubruh",
twitter_username: "riolubruh"
}
],
version: "1.9.7",
description: "Splits files larger than the upload limit into smaller chunks that can be redownloaded into a full file later.",
github: "https://github.com/riolubruh/SplitLargeFiles",
github_raw: "https://raw.githubusercontent.com/riolubruh/SplitLargeFiles/main/SplitLargeFiles.plugin.js"
},
changelog: [
{
title: "1.9.7",
items: [
"Improved YABDP4Nitro compatibility."
]
}
],
main: "bundled.js"
};
class Dummy {
constructor() { this._config = config; }
start() { }
stop() { }
}
if (!global.ZeresPluginLibrary) {
BdApi.showConfirmationModal("Library Missing", `The library plugin needed for ${config.name ?? config.info.name} is missing. Please click Download Now to install it.`, {
confirmText: "Download Now",
cancelText: "Cancel",
onConfirm: () => {
require("request").get("https://betterdiscord.app/gh-redirect?id=9", async (err, resp, body) => {
if (err) return require("electron").shell.openExternal("https://betterdiscord.app/Download?id=9");
if (resp.statusCode === 302) {
require("request").get(resp.headers.location, async (error, response, content) => {
if (error) return require("electron").shell.openExternal("https://betterdiscord.app/Download?id=9");
await new Promise(r => require("fs").writeFile(require("path").join(BdApi.Plugins.folder, "0PluginLibrary.plugin.js"), content, r));
});
}
else {
await new Promise(r => require("fs").writeFile(require("path").join(BdApi.Plugins.folder, "0PluginLibrary.plugin.js"), body, r));
}
});
}
});
}
module.exports = !global.ZeresPluginLibrary ? Dummy : (([Plugin, Api]) => {
const plugin = (Plugin, Library) => {
"use strict";
const { ContextMenu, Webpack, React } = BdApi;
const { byProps } = Webpack.Filters;
const { Logger, Patcher, DiscordModules, DOMTools, PluginUtilities, Settings, PluginUpdater } = Library;
const { SettingPanel, Slider } = Settings;
const { Dispatcher, SelectedChannelStore, SelectedGuildStore, UserStore, MessageStore, Permissions, ChannelStore, MessageActions } = DiscordModules;
const MessageAttachmentManager = Webpack.getModule(byProps("addFiles"));
const FileCheckMod = Webpack.getModule((m) => Object.values(m).filter((v) => v?.toString).map((v) => v.toString()).some((v) => v.includes("getCurrentUser();") && v.includes("getUserMaxFileSize")));
//const MessageAccessories = Webpack.getByKeys("MessageAccessories").MessageAccessories;
const MessageAccessories = Webpack.getByKeys("$p", "BB").BB;
//const Attachment = Webpack.getByKeys("isMediaAttachment", "default");
const Attachment = Webpack.getAllByKeys("Z", "g").filter(o => o.g.toString().includes("attachment"))[0];
const uploadinator = Webpack.getByKeys("G", "d");
//Hover Download button
const downloadButtonMod = Webpack.getAllByKeys("Z").filter(obj => obj.Z.toString().includes(`MEDIA_DOWNLOAD_BUTTON_TAPPED`))[0];
const BATCH_SIZE = 10;
const queuedUploads = /* @__PURE__ */ new Map();
const activeDownloads = /* @__PURE__ */ new Map();
let registeredDownloads = new Array();
/* async function downloadId(download) {
if (!download)
return null;
const encoder = new TextEncoder();
const digested = await crypto.subtle.digest("SHA-256", encoder.encode(download.urls.join("")));
return Buffer.from(digested).toString("base64");
} */
function getFunctionNameFromString(obj, search) {
for (const [k, v] of Object.entries(obj)) {
if (search.every((str) => v?.toString().match(str))) {
return k;
}
}
return null;
}
async function addFileProgress(download, progress) {
if (activeDownloads.has(download.messages[0].id)) {
activeDownloads.set(download.messages[0].id, activeDownloads.get(download.messages[0].id) + progress);
} else {
activeDownloads.set(download.messages[0].id, progress);
}
Dispatcher.dispatch({
type: "SLF_UPDATE_PROGRESS"
});
}
const concatTypedArrays = (a, b) => {
var c = new a.constructor(a.length + b.length);
c.set(a, 0);
c.set(b, a.length);
return c;
};
const isSetLinear = (set) => {
for (let setIndex = 0; setIndex < set.length; setIndex++) {
if (!set.has(setIndex)) {
return false;
}
}
return true;
};
async function downloadFiles(download) {
const https = require("https");
const fs = require("fs");
const path = require("path");
const electron = require("electron");
const vals = new Uint8Array(8);
crypto.getRandomValues(vals);
const id = Buffer.from(vals).toString("hex");
const tempFolder = path.join(process.env.TMP, `dlfc-download-${id}`);
fs.mkdirSync(tempFolder);
BdApi.showToast("Downloading files...", { type: "info" });
let promises = [];
for (const url of download.urls) {
let chunkName = url.slice(url.lastIndexOf("/") + 1);
chunkName = chunkName.slice(0, chunkName.indexOf("?"));
const dest = path.join(tempFolder, chunkName);
const file = fs.createWriteStream(dest);
const downloadPromise = new Promise((resolve, reject) => {
https.get(url, (response) => {
response.on("end", () => {
file.close();
resolve(chunkName);
});
response.on("data", (data) => {
file.write(Buffer.from(data));
addFileProgress(download, data.length);
});
}).on("error", (err) => {
fs.unlink(dest);
reject(err);
});
});
promises.push(downloadPromise);
}
let movelocation = await BdApi.UI.openDialog({
mode: "save",
title: "Save As",
showOverwriteConfirmation: true,
defaultPath: process.env.USERPROFILE + "\\Desktop\\" + download.filename
});
Promise.all(promises).then((names) => {
let fileBuffers = [];
for (let name of names) {
name = name.slice(0, name.indexOf("?"));
if (name.endsWith(".dlf")) {
name += "c";
}
fileBuffers.push(fs.readFileSync(path.join(tempFolder, name), null));
}
fileBuffers = fileBuffers.filter((buffer) => buffer.length >= 5 && buffer[0] === 223 && buffer[1] === 0);
fileBuffers.sort((left, right) => left[2] - right[2]);
let numChunks = 0;
let chunkSet = /* @__PURE__ */ new Set();
let outputFile = fs.createWriteStream(path.join(tempFolder, `${download.filename}`));
for (const buffer of fileBuffers) {
if (buffer[2] >= buffer[3] || numChunks !== 0 && buffer[3] > numChunks) {
BdApi.showToast("Reassembly failed: Some chunks are not part of the same file", { type: "error" });
outputFile.close();
return;
}
chunkSet.add(buffer[2]);
numChunks = buffer[3];
outputFile.write(buffer.subarray(4));
}
if (!isSetLinear(chunkSet) || chunkSet.size === 0) {
BdApi.showToast("Reassembly failed: Some chunks do not exist", { type: "error" });
outputFile.close();
return;
}
outputFile.close(() => {
BdApi.showToast("File reassembled successfully", { type: "success" });
fs.readdirSync(tempFolder).forEach(file => {
if (file.toString().includes(".dlfc")) {
fs.unlinkSync((tempFolder + "\\" + file.toString()));
}
});
/*
* Move the downloaded file to movelocation
* Most web browsers actually do something similar to this when downloading files
* (the download starts immediately and then you just choose where it gets saved)
* so this is actually fairly standard even though I had to do it for a completely different reason
*/
fs.rename((path.join(tempFolder, `${download.filename}`)), movelocation.filePath);
//electron.shell.showItemInFolder(path.join(tempFolder, `${download.filename}`));
BdApi.showToast(("File downloaded to " + (path.join(tempFolder, `${download.filename}`))), { type: "success", timeout: 5000 });
// downloadId(download).then((id2) => activeDownloads.delete(id2));
// activeDownloads.delete(download.messages[0].id);
Dispatcher.dispatch({
type: "SLF_UPDATE_PROGRESS"
});
fs.rmdirSync(tempFolder, { recursive: true });
});
}).catch((err) => {
Logger.error(err);
BdApi.showToast("Failed to download file. Check console for error output.", { type: "error" });
fs.rmdirSync(tempFolder, { recursive: true });
});
}
function FileIcon() {
return React.createElement("img", {
className: "dlfcIcon",
alt: "Attachment file type: SplitLargeFiles Chunk File",
title: "SplitLargeFiles Chunk File",
src: "data:image/svg+x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iMTAiIHN0eWxlPSJmaWxsOnJnYigxNDcsMTU1LDI0OSk7Ii8+CiAgICA8L2c+CiAgICA8ZyB0cmFuc2Zvcm09Im1hdHJpeCgxLDAsMCwwLjUsMjIsMjMuNSkiPgogICAgICAgIDxyZWN0IHg9IjIxIiB5PSIzOSIgd2lkdGg9IjEwIiBoZWlnaHQ9IjEwIiBzdHlsZT0iZmlsbDpyZ2IoMTQ3LDE1NSwyNDkpOyIvPgogICAgPC9nPgo8L3N2Zz4K"
});
}
/* class AttachmentShim extends React.Component {
constructor(props) {
super(props);
this.child = props.children;
this.attachmentID = props.attachmentData.id;
this.state = {
downloadData: null,
downloadProgress: 0
};
this.onNewDownload = this.onNewDownload.bind(this);
this.onDownloadProgress = this.onDownloadProgress.bind(this);
}
componentDidMount() {
Dispatcher.subscribe("DLFC_REFRESH_DOWNLOADS", this.onNewDownload);
Dispatcher.subscribe("SLF_UPDATE_PROGRESS", this.onDownloadProgress);
}
componentWillUnmount() {
Dispatcher.unsubscribe("DLFC_REFRESH_DOWNLOADS", this.onNewDownload);
Dispatcher.unsubscribe("SLF_UPDATE_PROGRESS", this.onDownloadProgress);
}
onNewDownload(e) {
if (this.state.downloadData) {
return;
}
for (const download of e.downloads) {
if (download.messages[0].attachmentID === this.attachmentID) {
this.setState({ downloadData: download });
break;
}
}
}
onDownloadProgress() {
downloadId(this.state.downloadData).then((id) => {
if (this.state.downloadData && activeDownloads.has(id)) {
this.setState({ downloadProgress: activeDownloads.get(id) / this.state.downloadData.totalSize });
} else {
this.setState({ downloadProgress: 0 });
}
});
}
render() {
if (this.state.downloadData) {
return React.createElement(Attachment[getFunctionNameFromString(Attachment, ["renderAdjacentContent"])], {
filename: this.state.downloadData.filename + (this.state.downloadProgress > 0 ? ` - Downloading ${Math.round(this.state.downloadProgress * 100)}%` : ""),
url: null,
dlfc: true,
size: this.state.downloadData.totalSize,
onClick: () => {
downloadFiles(this.state.downloadData);
}
}, []);
} else {
return this.child;
}
}
} */
const defaultSettingsData = {
deletionDelay: 6,
fileSplitSize: 26214400
};
let settings = null;
const reloadSettings = () => {
settings = PluginUtilities.loadSettings("SplitLargeFiles", defaultSettingsData);
};
const validActionDelays = [6, 7, 8, 9, 10, 11, 12];
class SplitLargeFiles extends Plugin {
onStart() {
BdApi.injectCSS("SplitLargeFiles", `
.dlfcIcon {
width: 30px;
height: 40px;
margin-right: 8px;
}
.slfClickable {
cursor: pointer;
background: none;
}
.slfIcon {
color: var(--interactive-normal);
}
.slfClickable:hover .slfIcon {
color: var(--interactive-hover);
}
`);
//explicitly trigger plugin update
PluginUpdater.checkForUpdate(this.getName(), this.getVersion(), this._config.info.github_raw);
//Disable 500MB (basically a safety check) limit.
const RealMaxFileSizeMod = Webpack.getByKeys("B", "Fm", "Lc", "zz");
RealMaxFileSizeMod.zz = Number.MAX_SAFE_INTEGER;
Patcher.instead(uploadinator, "d", (_, e) => {
try {
//console.log(e);
var E = Array.from(e[0]).map((function (e) {
return {
file: e,
platform: 1
}
}))
MessageAttachmentManager.addFiles({
files: E,
channelId: e[1].id,
showLargeMessageDialog: false,
draftType: e[2]
})
} catch (err) {
console.error(err);
BdApi.UI.showToast("An error occurred.", { type: "error" });
}
});
reloadSettings();
this.incompleteDownloads = [];
Patcher.instead(MessageAttachmentManager, "addFiles", (_, [{ files, channelId }], original) => {
let oversizedFiles = [], regularFiles = [];
for(let i = 0; i < files.length; i++){
const fileContainer = files[i];
if(fileContainer.file.clip != undefined){
continue;
}
if(fileContainer.file.type.startsWith("video/") && BdApi.Plugins.isEnabled("YABDP4Nitro")){
const YABDP4NitroSettings = BdApi.getData("YABDP4Nitro", "settings");
if(YABDP4NitroSettings.useClipBypass && fileContainer.file.size <= 104857600){
continue;
}
}
const [numChunks, numChunksWithHeaders] = this.calcNumChunks(fileContainer.file);
if (numChunks === 1) {
regularFiles.push(fileContainer);
continue;
} else if (numChunksWithHeaders > 255) {
BdApi.showToast("File size exceeds max chunk count of 255.", { type: "error" });
return;
}
oversizedFiles.push(fileContainer);
}
if (oversizedFiles.length === 0) {
original({
files: regularFiles,
channelId,
showLargeMessageDialog: false,
draftType: 0
});
} else {
this.splitLargeFiles(oversizedFiles).then((fileArrayArray) => {
if (fileArrayArray.length === 0) {
return;
}
MessageAttachmentManager.clearAll(this.getCurrentChannel()?.id, 0);
const fileArray = regularFiles.concat.apply([], fileArrayArray);
if (queuedUploads.has(channelId)) {
queuedUploads.get(channelId).push(fileArray);
} else {
queuedUploads.set(channelId, fileArray);
}
original({
files: queuedUploads.get(channelId).splice(0, BATCH_SIZE),
channelId,
showLargeMessageDialog: false,
draftType: 0
});
});
}
});
Patcher.after(MessageAccessories.prototype, "renderAttachments", (_, [arg], ret) => {
if (!ret || arg.attachments.length === 0 || !arg.attachments[0].filename.endsWith(".dlfc")) {
return;
}
/*const component = ret.props.children;
ret.props.children = React.createElement(AttachmentShim, {
attachmentData: arg.attachments[0]
}, component); */
});
Patcher.after(downloadButtonMod, "Z", (_, args, ret) => {
let registeredDownload = registeredDownloads.find((element) => element.urls.find((url) => url === args[0].href));
if(registeredDownload){
//disable default download
ret.props.href = undefined;
ret.props.onClick = (e) => {
e.stopPropagation();
downloadFiles(registeredDownload);
}
}
});
Dispatcher.subscribe("SLF_UPDATE_PROGRESS", this.updateProgress);
Patcher.after(Attachment, getFunctionNameFromString(Attachment, ["renderAdjacentContent"]), (_, args, ret) => {
ret.props.children[0].props.children[1].props.onClick = args[0].onClick; //????
if (args[0].fileName.endsWith(".dlfc")) {
//File Icon
ret.props.children[0].props.children[0] = /* @__PURE__ */ React.createElement(FileIcon, null);
//For readability
let fileAnchor = ret.props.children[0].props.children[1].props.children[0].props.children.props;
//also for readability. (me when no pass by ref keyword. those who know know)
let fileSize = ret.props.children[0].props.children[1].props.children[1].props;
//dont show split part number or DLFC file extension (the download doesnt get registered until you hover it)
fileAnchor.children = fileAnchor.children.replace(/^[0-99]-[0-99]_/, "").replace(/.dlfc$/, "");
let registeredDownload = registeredDownloads.find((element) => element.messages.find((message) => message.id === args[0].message.id));
if(registeredDownload) {
fileAnchor.children = registeredDownload.filename.replaceAll("_", " ");
fileSize.children = (registeredDownload.totalSize / 1024 / 1024).toFixed(2) + " MB • DLFC File";
//disable default download
fileAnchor.href = undefined;
//trigger SLF download on click
fileAnchor.onClick = (e) => {
e.stopPropagation();
downloadFiles(registeredDownload);
}
}
}
});
this.messageCreate = (e) => {
if (e.channelId === this.getCurrentChannel()?.id) {
if (queuedUploads.has(e.channelId) && e.message.author.id === UserStore.getCurrentUser().id) {
MessageAttachmentManager.addFiles({
files: queuedUploads.get(e.channelId).splice(0, BATCH_SIZE),
channelId: e.channelId
});
if (queuedUploads.get(e.channelId).length === 0) {
queuedUploads.delete(e.channelId);
}
}
setTimeout(() => this.findAvailableDownloads(), 500);
}
};
Dispatcher.subscribe("MESSAGE_CREATE", this.messageCreate);
this.channelSelect = (_) => {
setTimeout(() => this.findAvailableDownloads(), 200);
};
Dispatcher.subscribe("CHANNEL_SELECT", this.channelSelect);
this.loadMessagesSuccess = (_) => {
this.findAvailableDownloads();
};
Dispatcher.subscribe("LOAD_MESSAGES_SUCCESS", this.loadMessagesSuccess);
this.messageContextMenuUnpatch = ContextMenu.patch("message", (tree, props) => {
const incomplete = this.incompleteDownloads.find((download) => download.messages.some((message) => message.id === props.message.id));
const registered = registeredDownloads.find((download) => download.messages.some((msg) => msg.id === props.message.id));
if (!(incomplete || registered)) {
return;
}
tree.props.children[2].props.children.push(ContextMenu.buildItem({ type: "separator" }), ContextMenu.buildItem({
label: "Refresh Downloadables", action: () => {
this.findAvailableDownloads();
BdApi.showToast("Downloadables refreshed", { type: "success" });
}
}), ContextMenu.buildItem({
label: "Copy Download Links", action: () => {
const urls = this.getFileURLsFromMessageId(props.message.id);
if (!urls) {
BdApi.showToast("Failed to Copy Links", { type: "error" });
}
DiscordNative.clipboard.copy(urls.join(" "));
}
}));
if (incomplete && this.canDeleteDownload(incomplete)) {
tree.props.children[2].props.children.push(ContextMenu.buildItem({
label: "Delete Download Fragments", danger: true, action: () => {
this.deleteDownload(incomplete);
this.findAvailableDownloads();
}
}));
}
if (!incomplete) {
tree.props.children[2].props.children.push(ContextMenu.buildItem({
label: "Download Large File", action: () => {
downloadFiles(registered);
}
}));
}
});
this.channelContextMenuUnpatch = ContextMenu.patch("channel-context", (tree, _) => {
tree.props.children[2].props.children.push(ContextMenu.buildItem({ type: "separator" }), ContextMenu.buildItem({
label: "Refresh Downloadables", action: () => {
this.findAvailableDownloads();
BdApi.showToast("Downloadables refreshed", { type: "success" });
}
}));
});
this.userContextMenuUnpatch = ContextMenu.patch("user-context", (tree, _) => {
tree.props.children[2].props.children.push(ContextMenu.buildItem({ type: "separator" }), ContextMenu.buildItem({
label: "Refresh Downloadables", action: () => {
this.findAvailableDownloads();
BdApi.showToast("Downloadables refreshed", { type: "success" });
}
}));
});
this.messageDelete = (e) => {
if (e.channelId !== this.getCurrentChannel()?.id) {
return;
}
const download = registeredDownloads.find((element) => element.messages.find((message) => message.id === e.id));
if (download && this.canDeleteDownload(download)) {
this.deleteDownload(download, e.id);
}
this.findAvailableDownloads();
};
Dispatcher.subscribe("MESSAGE_DELETE", this.messageDelete);
Patcher.before(Webpack.getByKeys("Url", "resolve", "resolveObject"), "parse", (_, a) => {
//Fix crashing issue
a[0] = String(a[0]);
});
//BdApi.showToast("Waiting for BetterDiscord to load before refreshing downloadables...", { type: "info" });
setTimeout(() => {
//BdApi.showToast("Downloadables refreshed", { type: "success" });
this.findAvailableDownloads();
}, 100);
}
updateProgress(){
activeDownloads.keys().forEach(key => {
let registeredDownload = registeredDownloads.find((element) => element.messages.find((message) => message.id === key));
if(document.getElementById("message-accessories-" + key)){
try{
let element = document.getElementById("message-accessories-" + key);
let attachment = element.children[0].children[0].children[0].children[0].children[1].children[1];
attachment.innerHTML = (registeredDownload.totalSize / 1024 / 1024).toFixed(2) + " MB • DLFC File" + " • Downloading " + ( activeDownloads.get(key) / registeredDownload.totalSize * 100 ).toFixed(2) + "%";
if(attachment.innerHTML.includes("100.00%")){
attachment.innerHTML = attachment.innerHTML.replace("Downloading 100.00%", "Download Complete");
}
}catch(e){
console.error(e);
}
}
if(activeDownloads.get(key) == registeredDownload.totalSize){
activeDownloads.delete(key);
}
});
}
getFileURLsFromMessageId(messageId) {
const download = registeredDownloads.find((download2) => download2.messages.some((msg) => msg.id === messageId));
return download?.urls;
}
splitLargeFiles(fileContainers) {
BdApi.showToast("Generating file chunks...", { type: "info" });
let promises = [];
for (const fileContainer of fileContainers) {
const file = fileContainer.file;
promises.push(new Promise((res, rej) => {
file.arrayBuffer().then((buffer) => {
const fileBytes = new Uint8Array(buffer);
const [numChunks, numChunksWithHeaders] = this.calcNumChunks(file);
const fileList = [];
for (let chunk = 0; chunk < numChunksWithHeaders; chunk++) {
const baseOffset = chunk * (this.maxFileUploadSize() - 4);
const headerBytes = new Uint8Array(4);
headerBytes.set([223, 0, chunk & 255, numChunks & 255]);
const bytesToWrite = fileBytes.slice(baseOffset, baseOffset + this.maxFileUploadSize() - 4);
fileList.push({
file: new File([concatTypedArrays(headerBytes, bytesToWrite)], `${chunk}-${numChunks - 1}_${file.name}.dlfc`),
platform: fileContainer.platform
});
}
res(fileList);
}).catch((err) => {
Logger.error(err);
BdApi.showToast("Failed to read file, please try again later.", { type: "error" });
rej();
});
}));
}
return Promise.all(promises);
}
calcNumChunks(file) {
return [Math.ceil(file.size / this.maxFileUploadSize()), Math.ceil(file.size / (this.maxFileUploadSize() - 4))];
}
getSettingsPanel() {
reloadSettings();
const settingPanel = new SettingPanel(() => {
PluginUtilities.saveSettings("SplitLargeFiles", settings);
}, new Slider("Chunk File Deletion Delay", "How long to wait (in seconds) before deleting each sequential message of a chunk file. If you plan on deleting VERY large files you should set this value high to avoid API spam.", validActionDelays[0], validActionDelays[validActionDelays.length - 1], settings.deletionDelay, (newVal) => {
if (newVal > validActionDelays[validActionDelays.length - 1] || newVal < validActionDelays[0]) {
newVal = validActionDelays[0];
}
settings.deletionDelay = newVal;
}, { markers: validActionDelays, stickToMarkers: true }),
new Settings.Dropdown("File Split Size", "Changes the size of the split files.", settings.fileSplitSize, [
{ label: "8MB", value: 8387608 },
{ label: "10MB", value: 10485760 },
{ label: "25MB", value: 26214400 },
{ label: "50MB", value: 52428800 },
{ label: "100MB", value: 104857600 },
{ label: "500MB", value: 524288000 }], value => settings.fileSplitSize = value, { searchable: true }
)
).getElement();
settingPanel.style.paddingBottom = "75px"
return settingPanel
}
maxFileUploadSize() {
return settings.fileSplitSize
}
findAvailableDownloads() {
registeredDownloads = [];
this.incompleteDownloads = [];
for (const message of this.getChannelMessages(this.getCurrentChannel()?.id) ?? []) {
if (message.noDLFC) {
continue;
}
let foundDLFCAttachment = false;
for (const attachment of message.attachments) {
if (isNaN(parseInt(attachment.filename)) || !attachment.filename.endsWith(".dlfc")) {
continue;
}
foundDLFCAttachment = true;
const realName = this.extractRealFileName(attachment.filename);
const existingEntry = registeredDownloads.find((element) => element.filename === realName && !element.foundParts.has(parseInt(attachment.filename)));
if (existingEntry) {
existingEntry.urls.push(attachment.url);
existingEntry.messages.push({ id: message.id, date: message.timestamp, attachmentID: attachment.id });
existingEntry.foundParts.add(parseInt(attachment.filename));
existingEntry.totalSize += attachment.size;
} else {
registeredDownloads.unshift({
filename: realName,
owner: message.author.id,
urls: [attachment.url],
messages: [{ id: message.id, date: message.timestamp, attachmentID: attachment.id }],
foundParts: /* @__PURE__ */ new Set([parseInt(attachment.filename)]),
totalSize: attachment.size
});
}
}
if (!foundDLFCAttachment) {
message.noDLFC = true;
}
}
registeredDownloads = registeredDownloads.filter((value, _, __) => {
const chunkSet = /* @__PURE__ */ new Set();
let highestChunk = 0;
for (const url of value.urls) {
const filename = url.slice(url.lastIndexOf("/") + 1);
const fileNumber = parseInt(filename);
const fileTotal = parseInt(filename.slice(filename.indexOf("-") + 1));
chunkSet.add(fileNumber);
if (highestChunk === 0) {
highestChunk = fileTotal;
} else if (highestChunk !== fileTotal) {
this.incompleteDownloads.push(value);
return false;
}
}
const result = isSetLinear(chunkSet) && highestChunk + 1 === chunkSet.size;
if (!result) {
this.incompleteDownloads.push(value);
}
return result;
});
registeredDownloads.forEach((download) => {
download.messages.sort((first, second) => first.date - second.date);
for (let messageIndex = 1; messageIndex < download.messages.length; messageIndex++) {
if (download.messages[messageIndex].id === download.messages[0].id) {
this.setAttachmentVisibility(download.messages[0].id, messageIndex, false);
} else {
this.setMessageVisibility(download.messages[messageIndex].id, false);
}
}
});
if (registeredDownloads.length > 0) {
Dispatcher.dispatch({
type: "DLFC_REFRESH_DOWNLOADS",
downloads: registeredDownloads
});
}
}
extractRealFileName(name) {
return name.slice(name.indexOf("_") + 1, name.length - 5);
}
setMessageVisibility(id, visible) {
const element = DOMTools.query('#chat-messages-' + BdApi.findModuleByProps("getLastChannelFollowingDestination").getChannelId() + '-' + id);
if (element) {
if (visible) {
element.removeAttribute("hidden");
} else {
element.setAttribute("hidden", "");
}
} else {
Logger.warn('Unable to find DOM object with selector #chat-messages-' + BdApi.findModuleByProps("getLastChannelFollowingDestination").getChannelId() + '-' + id);
}
}
setAttachmentVisibility(id, index, visible) {
const parent = DOMTools.query('#message-accessories-' + id);
let element = parent?.lastChild?.children[index];
if (element) {
if (visible) {
parent.removeAttribute("style");
element.removeAttribute("style");
} else {
parent.setAttribute("style", "grid-row-gap: 0;");
element.setAttribute("style", "display: none;");
}
} else {
Logger.error(`Unable to find child DOM object at index ${index} with parent selector #message-accessories-${id}`);
}
}
deleteDownload(download, excludeMessage = null) {
if (download.messages.map((msg) => msg.id).every((id, i, arr) => id === arr[0])) {
return;
}
BdApi.showToast(`Deleting chunks (1 chunk/${settings.deletionDelay} seconds)`, { type: "success" });
let delayCount = 1;
for (const message of this.getChannelMessages(this.getCurrentChannel().id)) {
const downloadMessage = download.messages.find((dMessage) => dMessage.id === message.id);
if (downloadMessage) {
if (excludeMessage && message.id === excludeMessage.id) {
continue;
}
this.setMessageVisibility(message.id, true);
const downloadMessageIndex = download.messages.indexOf(downloadMessage);
download.messages.splice(downloadMessageIndex, 1);
setTimeout(() => this.deleteMessage(message), delayCount * settings.deletionDelay * 1e3);
delayCount += 1;
}
}
}
canDeleteDownload(download) {
return download.owner === UserStore.getCurrentUser().id || this.canManageMessages();
}
getCurrentChannel() {
return ChannelStore.getChannel(SelectedChannelStore.getChannelId()) ?? null;
}
getChannelMessages(channelId) {
if (!channelId) {
return null;
}
return MessageStore.getMessages(channelId)._array;
}
canManageMessages() {
const currentChannel = this.getCurrentChannel();
if (!currentChannel) {
return false;
}
return !!(Permissions.computePermissions(currentChannel) & 0x2000n);
}
deleteMessage(message) {
try {
MessageActions.deleteMessage(message.channel_id, message.id, false);
} catch (err) {
console.error(err);
}
}
onStop() {
Patcher.unpatchAll();
if (this.messageContextMenuUnpatch)
this.messageContextMenuUnpatch();
if (this.channelContextMenuUnpatch)
this.channelContextMenuUnpatch();
if (this.userContextMenuUnpatch)
this.userContextMenuUnpatch();
Dispatcher.unsubscribe("MESSAGE_CREATE", this.messageCreate);
Dispatcher.unsubscribe("CHANNEL_SELECT", this.channelSelect);
Dispatcher.unsubscribe("MESSAGE_DELETE", this.messageDelete);
Dispatcher.unsubscribe("LOAD_MESSAGES_SUCCESS", this.loadMessagesSuccess);
Dispatcher.unsubscribe("SLF_UPDATE_PROGRESS", this.updateProgress);
BdApi.clearCSS("SplitLargeFiles");
}
}
;
return SplitLargeFiles;
};
return plugin(Plugin, Api);
})(global.ZeresPluginLibrary.buildPlugin(config));
/*@end@*/