MCPcopy Create free account
hub / github.com/fontsource/fontsource / putBucket

Function putBucket

api/download/src/bucket.ts:212–270  ·  view source on GitHub ↗
(
	bucketPath: string,
	body: Uint8Array | ArrayBuffer,
)

Source from the content-addressed store, hash-verified

210};
211
212export const putBucket = async (
213 bucketPath: string,
214 body: Uint8Array | ArrayBuffer,
215) => {
216 // We only use multipart uploads for files larger than 80MB since Cloudflare
217 // limits the maximum request size to 100MB
218 const partSize = 80 * 1024 * 1024;
219 if (body.byteLength < partSize) {
220 keepAwake(SLEEP_MINUTES);
221 const resp = await fetch(
222 `https://upload.fontsource.org/put/${bucketPath}`,
223 {
224 method: 'PUT',
225 headers: {
226 Authorization: `Bearer ${
227 // biome-ignore lint/style/noNonNullAssertion: <explanation>
228 process.env.UPLOAD_KEY!
229 }`,
230 },
231 body,
232 },
233 );
234
235 if (!resp.ok) {
236 const error = await resp.text();
237 handleBucketError(resp, `Unable to upload file ${bucketPath}. ${error}`);
238 }
239
240 return;
241 }
242
243 info(`Uploading ${bucketPath} in parts with size ${body.byteLength}`);
244
245 const uploadId = await initiateMultipartUpload(bucketPath);
246
247 const parts: R2UploadedPart[] = [];
248 let offset = 0;
249 let partNumber = 1;
250
251 // Upload buffers in parts
252 while (offset < body.byteLength) {
253 const end = Math.min(offset + partSize, body.byteLength);
254 const partData = body.slice(offset, end);
255 info(`Uploading part ${partNumber} with size ${partData.byteLength}`);
256
257 const etag = await uploadPart(bucketPath, uploadId, partNumber, partData);
258
259 parts.push({
260 etag,
261 partNumber,
262 });
263
264 offset = end;
265 partNumber++;
266 }
267
268 // Complete multipart upload
269 await completeMultipartUpload(bucketPath, uploadId, parts);

Callers 3

downloadFileFunction · 0.90
downloadVariableFileFunction · 0.90
generateZipFunction · 0.90

Calls 7

keepAwakeFunction · 0.90
handleBucketErrorFunction · 0.85
initiateMultipartUploadFunction · 0.85
uploadPartFunction · 0.85
completeMultipartUploadFunction · 0.85
textMethod · 0.65
fetchFunction · 0.50

Tested by

no test coverage detected