-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathupload.utils.ts
More file actions
63 lines (58 loc) · 2.35 KB
/
Copy pathupload.utils.ts
File metadata and controls
63 lines (58 loc) · 2.35 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
import { UsageService } from '../services/usage.service';
import { FormatUtils } from './format.utils';
import { Readable } from 'node:stream';
import { BufferStream } from './stream.utils';
import { ThumbnailUtils } from './thumbnail.utils';
import { CLIUtils } from './cli.utils';
const DEFAULT_MAX_FILE_SIZE = 100 * 1024 * 1024 * 1024;
export class UploadUtils {
static readonly checkUploadSizeLimits = async (size: number): Promise<void> => {
const limits = await UsageService.instance.fetchLimits();
if (limits?.maxUploadFileSize && size > limits.maxUploadFileSize) {
const formattedSize = FormatUtils.humanFileSize(size);
const formattedLimit = FormatUtils.humanFileSize(limits.maxUploadFileSize);
throw new Error(`File is too big (${formattedSize} exceeds account upload limit of ${formattedLimit})`);
}
if (size > DEFAULT_MAX_FILE_SIZE) {
//Default limit if limits are not set from backend
throw new Error('File is too big (more than 100 GB)');
}
};
static readonly prepareUploadStreams = (
readable: Readable,
fileType: string,
size: number,
): {
fileStream: Readable;
thumbnailStream: BufferStream | undefined;
isThumbnailable: boolean;
} => {
const isThumbnailable = ThumbnailUtils.isImageThumbnailable(fileType, size);
if (!isThumbnailable) {
return { fileStream: readable, thumbnailStream: undefined, isThumbnailable };
}
const bufferStream = new BufferStream();
const fileStream = readable.pipe(bufferStream);
return { fileStream, thumbnailStream: bufferStream, isThumbnailable };
};
static readonly getTimings = (
size: number,
timings: {
networkUpload: number;
driveUpload: number;
thumbnailUpload: number;
},
): {
totalTime: number;
throughputMBps: number;
timingBreakdown: string;
} => {
const totalTime = Object.values(timings).reduce((sum, time) => sum + time, 0);
const throughputMBps = CLIUtils.calculateThroughputMBps(size, timings.networkUpload);
const timingBreakdown =
`Network upload: ${CLIUtils.formatDuration(timings.networkUpload)} (${throughputMBps.toFixed(2)} MB/s)\n` +
`Drive upload: ${CLIUtils.formatDuration(timings.driveUpload)}\n` +
`Thumbnail: ${CLIUtils.formatDuration(timings.thumbnailUpload)}\n`;
return { totalTime, throughputMBps, timingBreakdown };
};
}