Files
discord-decomp/lib/uploader/UploaderBase.tsx
T
2026-11-01 12:12:55 +02:00

423 lines
14 KiB
TypeScript

// Metro module 6151 (f6152).
import { AbortCodes } from "../../Constants";
import { FileUploadErrorTypes } from "../../modules/messages/MessageConstants";
import Logger from "../../modules/debug/Logger";
import { EventEmitter } from "../../_bundle/EventEmitter_652";
import Module12 from "../../_bundle/Module12";
import { getUploadTarget } from "./UploadTargets";
import { getKestrelConfig, getEffectiveKestrelLimit } from "../../modules/media_uploads/experiments/KestrelExperiment";
import { calculateProgress } from "./UploadUtils";
import { cancelGetAttachmentFile } from "../../utils/AttachmentFile.native";
import { HermesBuiltin } from "../../_hermes/prelude";
let c3893_7;
let env3893 = {};
const c3893_6 = new Logger("UploaderBase.tsx");
class UploaderBase extends EventEmitter {
_aborted = false;
_errored = false;
files = [];
_lastUpdate = 0;
_loaded = 0;
alreadyStarted = false;
_handleStart = (a0) => {
this._cancel = a0;
if (!this.alreadyStarted) {
this.emit("start", this._file);
}
this.alreadyStarted = true;
};
_handleProgress = (a0, a1, a2) => {
const _lastUpdate = Date.now();
const progress = calculateProgress(a0, a1);
const r11 = Math;
const rate = r11.floor((a0 - this._loaded) / ((_lastUpdate - this._lastUpdate) / 1000));
if (a2 != null) {
const items = this._file.items;
if (items != null) {
items.forEach((item) => {
item.item.progress = a2[item.id];
});
}
}
this._lastUpdate = _lastUpdate;
this._loaded = a0;
this._file = {
...this._file,
currentSize: a1,
progress,
rate
};
this.emit("progress", this._file);
};
_handleException = (a0) => {
this._handleError({
reason: {
type: FileUploadErrorTypes.ERROR_SOURCE_UNKNOWN,
msg: a0.toString()
}
});
};
_handleAborted = () => {
this.clearProcessingMessageInterval();
};
_handleError = ({ code, reason, body }) => {
this.clearProcessingMessageInterval();
if (!this._aborted) {
this._errored = true;
c3893_6.log(`_handleError: ${code} (${JSON.stringify(reason)}) for ${this.id}`);
this.emit("error", this._file, code, body, reason);
this.removeAllListeners();
}
};
_handleComplete = (a0) => {
this.clearProcessingMessageInterval();
c3893_6.log(`_handleComplete for ${this.id}`);
this.emit("complete", this._file, a0);
this.removeAllListeners();
};
id = Module12.uniqueId("Uploader");
_file = {
id: this.id,
currentSize: 0,
totalPreCompressionSize: 0,
compressionProgress: 0,
progress: 0,
rate: 0,
hasImage: false,
hasVideo: false,
attachmentsCount: 0,
items: void 0
};
constructor() {
super();
}
_fileSize() {
return this.files.reduce((acc, { currentSize: currentSize3 }) => {
if (currentSize3 == null) {
currentSize3 = 0;
}
return acc + currentSize3;
}, 0);
}
async compressAndCheckFileSize() {
let r39, r110, r207;
const _this = this;
let c25722_0, files, currentSize, currentSize2, c25722_6;
let c25722_7 = 0;
let c29674_0, c29674_1, c29674_2, c29674_3, c29674_4;
L43: {
L42: {
L41: {
L35: {
L34: {
L37: {
try {
c29674_0 = void 0;
c29674_1 = void 0;
c29674_2 = void 0;
c29674_3 = void 0;
const r24 = _this.files[0];
const uploadTarget = getUploadTarget(r24 == null ? void 0 : r24.item?.target);
c29674_0 = uploadTarget;
if (_this.files.length > uploadTarget.getMaxAttachmentsCount()) {
c3893_6.log(`Too many attachments for ${_this.id}`);
_this._handleError({
code: AbortCodes.TOO_MANY_ATTACHMENTS
});
break L43;
}
c29674_1 = getKestrelConfig({
location: "UploaderBase.compressAndCheckFileSize"
});
c3893_6.log(`compressing files for ${_this.id}`);
const files2 = _this.files;
files = files2;
const r96 = files2[Symbol.iterator]();
files = r96.next;
c25722_0 = r96;
const r98 = files;
let r99 = c25722_0;
const r100 = r99 === void 0 ? void 0 : (r39 = r98.call(r99)).done ? r99 = void 0 : r39.value;
c25722_0 = r99;
if (r99 === void 0) {
r110 = !(_this._fileSize() > c29674_0.getMaxTotalAttachmentSize());
if (r110) {
break L42;
}
_this._handleError({
code: AbortCodes.ENTITY_TOO_LARGE,
reason: {
type: FileUploadErrorTypes.POSTCOMPRESSION_SUM_TOO_LARGE
}
});
break L41;
}
c29674_2 = r100;
c25722_7 = 2;
await c29674_2.reactNativeCompressAndExtractData();
let r133 = "compressAndCheckFileSize() file has been cancelled for compression - ";
while (true) {
if (c29674_2.isCancelled()) {
c3893_6.log(HermesInternal.concat.call(r133, c29674_2.id));
c25722_7 = 0;
} else {
const currentSize3 = c29674_2.currentSize;
currentSize = currentSize3;
if (currentSize3 == null) {
currentSize = 0;
}
if (currentSize === 0) {
_this._handleError({
code: AbortCodes.ENTITY_EMPTY
});
c25722_7 = 0;
const r162 = c25722_0;
if (r162 !== void 0 && typeof r162.return === "function") {
r162.return();
}
break L37;
}
c29674_3 = getEffectiveKestrelLimit(c29674_1, c29674_0.getMaxFileSize(c29674_2.channelId));
const currentSize4 = c29674_2.currentSize;
currentSize2 = currentSize4;
if (currentSize4 == null) {
currentSize2 = 0;
}
if (currentSize2 > c29674_3) {
_this._handleError({
code: AbortCodes.ENTITY_TOO_LARGE,
reason: {
type: FileUploadErrorTypes.POSTCOMPRESSION_INDIVIDUAL_FILE_TOO_LARGE
}
});
c25722_7 = 0;
const r193 = c25722_0;
if (r193 !== void 0 && typeof r193.return === "function") {
r193.return();
}
break;
}
c25722_7 = 0;
}
const r195 = files;
let r196 = c25722_0;
const r197 = r196 === void 0 ? void 0 : (r39 = r195.call(r196)).done ? r196 = void 0 : r39.value;
c25722_0 = r196;
if (r196 === void 0) {
r207 = !(_this._fileSize() > c29674_0.getMaxTotalAttachmentSize());
if (r207) {
break L35;
}
_this._handleError({
code: AbortCodes.ENTITY_TOO_LARGE,
reason: {
type: FileUploadErrorTypes.POSTCOMPRESSION_SUM_TOO_LARGE
}
});
break L34;
}
c29674_2 = r197;
c25722_7 = 2;
await c29674_2.reactNativeCompressAndExtractData();
r133 = "compressAndCheckFileSize() file has been cancelled for compression - ";
}
} catch ($e) {
const r47 = $e;
let r8 = r47;
c25722_6 = r47;
let r48 = c25722_7;
if (r48 !== 0) {
while (true) {
if (r48 !== 1) {
try {
const r266 = c25722_6;
c25722_7 = 1;
c29674_4 = r266;
_this._handleException(c29674_4);
c25722_7 = 0;
const r272 = c25722_0;
if (r272 !== void 0 && typeof r272.return === "function") {
r272.return();
}
} catch (r263) {
r8 = r263;
c25722_6 = r263;
r48 = c25722_7;
if (r48 === 0) {
break;
}
continue;
}
return false;
}
try {
const r277 = c25722_6;
c25722_7 = 0;
const r279 = c25722_0;
if (r279 !== void 0 && typeof r279.return === "function") {
try {
r279.return();
} catch {}
}
throw r277;
} catch (r276) {
r8 = r276;
c25722_6 = r276;
r48 = c25722_7;
if (r48 === 0) {
break;
}
}
}
}
throw r8;
}
return false;
}
return false;
}
return 0;
}
return r207;
}
return 0;
}
return r110;
}
return false;
}
setUploadingTextForUI() {
const hasImage = this.files.some((file) => file.isImage);
const hasVideo = this.files.some((file) => file.isVideo);
const totalPostCompressionSize = this._fileSize();
c3893_6.log(`setUploadingTextForUI - total content: ${totalPostCompressionSize} bytes and ${this.files.length} attachments for ${this.id}`);
this._file = {
...this._file,
totalPostCompressionSize,
currentSize: totalPostCompressionSize,
hasVideo,
hasImage,
attachmentsCount: this.files.length,
items: this.files
};
}
_recomputeProgress() {
const r6 = this._recomputeProgressTotal();
this._handleProgress(r6.loaded, r6.total, this._recomputeProgressByFile());
}
_recomputeProgressTotal() {
const total = this._fileSize();
return {
loaded: this.files.reduce((acc, { loaded }) => {
if (loaded == null) {
loaded = 0;
}
return acc + loaded;
}, 0),
total
};
}
_recomputeProgressByFile() {
const r0 = {};
let c15524_0 = r0;
this.files.forEach((file) => {
c15524_0[file.id] = calculateProgress(file.loaded, file.currentSize);
});
return r0;
}
_addAttachmentsToPayload(a0, a1, a2) {
const r5 = {
...a0
};
return Module12.set(r5, a1, [...Module12.get(r5, a1, []), ...a2]);
}
clearProcessingMessageInterval() {
if (this.processingMessageChangeInterval != null) {
clearInterval(this.processingMessageChangeInterval);
this.processingMessageChangeInterval = void 0;
}
}
cancel() {
c3893_6.log(`cancel() for ${this.id}`);
if (!this._aborted) {
this._aborted = true;
const _cancel = this._cancel;
if (_cancel != null) {
_cancel.call(this);
}
this.files.forEach((file) => file.cancel());
this._handleComplete();
}
}
async cancelItem(a0) {
let env15525 = {
$parent: env3893
};
env15525[0] = a0;
env15525[1] = this;
let c25723_0, c25723_1, c25723_2;
L15: {
L14: {
let env29675;
const r14 = env15525;
c25723_2 = r14;
const r10 = env29675 = {
$parent: r14
};
c25723_1 = r10;
env29675[0] = void 0;
c3893_6.log(`Cancel called for ${env15525[1].id} for item ${env15525[0]}`);
const r41 = env15525[1].files.find((file) => file.id === env15525[0]);
env29675[0] = r41;
if (r41 == null) {
break L15;
}
if (r41.isCancelled()) {
break L15;
}
const r49 = env15525[1].files.indexOf(r41);
const r50 = env15525[1];
c25723_0 = 0;
const r22 = env15525[1].files.slice(0, r49);
const files2 = [];
c25723_0 = HermesBuiltin.arraySpread(files2, r22, c25723_0);
c25723_0 = HermesBuiltin.arraySpread(files2, env15525[1].files.slice(r49 + 1), c25723_0);
r50.files = files2;
env15525[1]._file = {
...env15525[1]._file,
items: env15525[1].files
};
await cancelGetAttachmentFile(r41);
env29675[0].cancel();
env15525[1].emit("cancel-upload-item", env15525[1]._file);
if (env15525[1].files.length !== 0) {
break L14;
}
env15525[1].cancel();
}
return void 0;
}
}
upload(a0) {
if (this._cancel != null) {
throw new Error("Uploader.upload(...): An upload is already in progress.");
}
this._lastUpdate = Date.now();
this._loaded = 0;
this._file = {
id: this.id,
currentSize: 0,
totalPreCompressionSize: 0,
compressionProgress: 0,
progress: 0,
rate: 0,
hasImage: false,
hasVideo: false,
attachmentsCount: 0,
items: a0
};
}
}
c3893_7 = UploaderBase;
export default UploaderBase;