-
Notifications
You must be signed in to change notification settings - Fork 789
Expand file tree
/
Copy pathfileUpload.ts
More file actions
157 lines (144 loc) · 4.96 KB
/
Copy pathfileUpload.ts
File metadata and controls
157 lines (144 loc) · 4.96 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as buffer from 'buffer';
import * as path from 'path';
import * as vscode from 'vscode';
import { GitHubRepository } from './githubRepository';
import Logger from '../common/logger';
import { formatError } from '../common/utils';
export interface FileUploadPlaceholder {
uri: vscode.Uri;
name: string;
placeholder: string;
}
export interface PendingFileUpload {
name: string;
placeholder: string;
getBytes(): Thenable<Uint8Array>;
}
/**
* Decode a base64 string to a {@linkcode Uint8Array}.
*/
export function decodeBase64(input: string): Uint8Array {
return buffer.Buffer.from(input, 'base64');
}
/**
* Guess a file extension (including the dot) for a given MIME type, falling back
* to an empty string when no good guess is available.
*/
export function guessExtensionFromMime(mimeType: string): string {
const lower = mimeType.toLowerCase();
switch (lower) {
case 'image/png': return '.png';
case 'image/jpeg': return '.jpg';
case 'image/gif': return '.gif';
case 'image/webp': return '.webp';
case 'image/svg+xml': return '.svg';
case 'image/bmp': return '.bmp';
case 'image/heic': return '.heic';
case 'video/mp4': return '.mp4';
case 'video/quicktime': return '.mov';
case 'video/webm': return '.webm';
case 'application/pdf': return '.pdf';
case 'application/zip': return '.zip';
case 'application/json': return '.json';
case 'text/plain': return '.txt';
case 'text/markdown': return '.md';
default: return '';
}
}
/**
* Compute placeholder strings for the given file names, deduplicating
* by name with `(2)`, `(3)` suffixes.
*/
export function placeholdersForNames(names: readonly string[]): { name: string; placeholder: string }[] {
const used = new Map<string, number>();
return names.map(name => {
const count = used.get(name) ?? 0;
used.set(name, count + 1);
const placeholder = count === 0
? `<!-- Uploading ${name} -->`
: `<!-- Uploading ${name} (${count + 1}) -->`;
return { name, placeholder };
});
}
/**
* Prompt the user for files to upload and compute the placeholder text that
* should be inserted into a comment textarea while the uploads run.
* Returns `undefined` when the user cancels.
*/
export async function pickFilesForUpload(): Promise<FileUploadPlaceholder[] | undefined> {
const fileUris = await vscode.window.showOpenDialog({
canSelectMany: true,
canSelectFiles: true,
canSelectFolders: false,
openLabel: vscode.l10n.t('Upload'),
title: vscode.l10n.t('Select files to upload'),
});
if (!fileUris || fileUris.length === 0) {
return undefined;
}
const names = fileUris.map(uri => path.basename(uri.fsPath));
const placeholders = placeholdersForNames(names);
return fileUris.map((uri, i) => ({ uri, name: placeholders[i].name, placeholder: placeholders[i].placeholder }));
}
/**
* Maximum number of file uploads to run in parallel. Limiting concurrency
* avoids memory and network spikes when many files are uploaded at once.
*/
const MAX_CONCURRENT_UPLOADS = 3;
/**
* Run the actual file uploads with limited concurrency, invoking the supplied
* callbacks as each upload finishes (or fails).
*/
export function runFileUploads(
githubRepository: GitHubRepository,
uploads: readonly FileUploadPlaceholder[],
logId: string,
onComplete: (placeholder: string, name: string, markdown: string) => void | Promise<void>,
onError: (placeholder: string, name: string, error: string) => void | Promise<void>,
): void {
runPendingUploads(
githubRepository,
uploads.map(u => ({
name: u.name,
placeholder: u.placeholder,
getBytes: () => vscode.workspace.fs.readFile(u.uri),
})),
logId,
onComplete,
onError,
);
}
/**
* Run uploads in parallel, fetching the bytes lazily via {@linkcode PendingFileUpload.getBytes}.
*/
export function runPendingUploads(
githubRepository: GitHubRepository,
uploads: readonly PendingFileUpload[],
logId: string,
onComplete: (placeholder: string, name: string, markdown: string) => void | Promise<void>,
onError: (placeholder: string, name: string, error: string) => void | Promise<void>,
): void {
let next = 0;
const runOne = async (): Promise<void> => {
while (next < uploads.length) {
const u = uploads[next++];
(async () => {
const bytes = await u.getBytes();
return githubRepository.uploadFileBytes(bytes, u.name);
})().then(markdown => {
return onComplete(u.placeholder, u.name, markdown);
}).catch(err => {
Logger.error(`Failed to upload file ${u.name}: ${formatError(err)}`, logId);
return onError(u.placeholder, u.name, formatError(err));
});
}
};
const workerCount = Math.min(MAX_CONCURRENT_UPLOADS, uploads.length);
for (let i = 0; i < workerCount; i++) {
void runOne();
}
}