-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathuploadFile.ts
More file actions
133 lines (117 loc) · 4.29 KB
/
Copy pathuploadFile.ts
File metadata and controls
133 lines (117 loc) · 4.29 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
import { StorageTypes } from '@internxt/sdk/dist/drive';
import { Network } from 'app/drive/services/network.service';
import { DriveFileData } from 'app/drive/types';
import { SdkFactory } from '../../../core/factory/sdk';
import localStorageService from '../../../core/services/local-storage.service';
import navigationService from '../../../core/services/navigation.service';
import workspacesService from '../../../core/services/workspace.service';
import { AppView } from '../../../core/types';
import notificationsService, { ToastType } from '../../../notifications/services/notifications.service';
import { getEnvironmentConfig } from '../network.service';
import { generateThumbnailFromFile } from '../thumbnail.service';
import { OwnerUserAuthenticationData } from '../../../network/UploadManager';
import { FileToUpload } from './types';
export interface FileUploadOptions {
isTeam: boolean;
abortController?: AbortController;
ownerUserAuthenticationData?: OwnerUserAuthenticationData;
abortCallback?: (abort?: () => void) => void;
isUploadedFromFolder?: boolean;
}
class RetryableFileError extends Error {
constructor(public file: FileToUpload) {
super('Retryable file');
this.name = 'RetryableFileError';
}
}
export async function uploadFile(
userEmail: string,
file: FileToUpload,
updateProgressCallback: (progress: number) => void,
options: FileUploadOptions,
continueUploadOptions: {
taskId: string;
isPaused: boolean;
isRetriedUpload: boolean;
},
): Promise<DriveFileData> {
const { bridgeUser, bridgePass, encryptionKey, bucketId } =
options.ownerUserAuthenticationData ?? getEnvironmentConfig(options.isTeam);
if (!bucketId) {
notificationsService.show({ text: 'Login again to start uploading files', type: ToastType.Warning });
localStorageService.clear();
navigationService.push(AppView.Login);
throw new Error('Bucket not found!');
}
const [promise, abort] = new Network(bridgeUser, bridgePass, encryptionKey).uploadFile(
bucketId,
{
filecontent: file.content,
filesize: file.size,
progressCallback: (progress) => {
updateProgressCallback(progress);
},
isUploadedFromFolder: options.isUploadedFromFolder,
},
continueUploadOptions,
);
options.abortCallback?.(abort?.abort);
const fileId = await promise;
if (fileId === undefined) throw new RetryableFileError(file);
const workspaceId = options?.ownerUserAuthenticationData?.workspaceId;
const workspacesToken = options?.ownerUserAuthenticationData?.workspacesToken;
const resourcesToken = options?.ownerUserAuthenticationData?.resourcesToken;
const isWorkspacesUpload = workspaceId && workspacesToken;
let response;
if (isWorkspacesUpload) {
const date = new Date();
const workspaceFileEntry = {
name: file.name,
bucket: bucketId,
fileId: fileId,
encryptVersion: StorageTypes.EncryptionVersion.Aes03,
folderUuid: file.parentFolderId,
size: file.size,
plainName: file.name,
type: file.type,
modificationTime: date.toISOString(),
date: date.toISOString(),
};
response = await workspacesService.createFileEntry(workspaceFileEntry, workspaceId, resourcesToken);
} else {
const storageClient = SdkFactory.getNewApiInstance().createNewStorageClient();
const fileEntry: StorageTypes.FileEntryByUuid = {
id: fileId,
type: file.type,
size: file.size,
name: file.name,
plain_name: file.name,
bucket: bucketId,
folder_id: file.parentFolderId,
encrypt_version: StorageTypes.EncryptionVersion.Aes03,
};
response = await storageClient.createFileEntryByUuid(fileEntry, options.ownerUserAuthenticationData?.token);
}
if (!response.thumbnails) {
response = {
...response,
thumbnails: [],
};
}
const generatedThumbnail = await generateThumbnailFromFile(
file,
response.id,
response.uuid,
userEmail,
options.isTeam,
);
if (generatedThumbnail?.thumbnail) {
response.thumbnails.push(generatedThumbnail.thumbnail);
if (generatedThumbnail.thumbnailFile) {
generatedThumbnail.thumbnail.urlObject = URL.createObjectURL(generatedThumbnail.thumbnailFile);
response.currentThumbnail = generatedThumbnail.thumbnail;
}
}
return response;
}
export default uploadFile;