Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions examples/completed-retry-guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { File, UploadxResponse } from '@uploadx/core';
import { RequestHandler } from 'express';
/* eslint-disable @typescript-eslint/no-namespace */
declare global {
namespace Express {
interface Response {
finishUpload: (uploadxResponse: UploadxResponse) => this;
}
}
}
/**
*
* @param busyResponse
* @param timeout seconds
*/
export const completedRetryGuard: (
busyResponse: UploadxResponse,
timeout?: number
) => RequestHandler = (busyResponse, timeout = 100) => {
const lock: Record<string, UploadxResponse> = {};

return async (req, res, next) => {
const { name: filename } = req.body as File;
function send(response: UploadxResponse) {
const { statusCode = 200, ...body } = response;
res.status(statusCode).json(body);
}
const done = () =>
new Promise<UploadxResponse>(resolve => {
let i = 0;
(function scan() {
if (lock[filename].statusCode !== busyResponse.statusCode || i++ > timeout) {
return resolve(lock[filename]);
}
setTimeout(scan, 1000);
})();
});

res.finishUpload = (uploadxResponse: UploadxResponse) => {
lock[filename] = uploadxResponse;
return res;
};

if (lock[filename]) {
send(await done());
return;
}

lock[filename] = busyResponse;
// prevent resend last chunk
send(busyResponse);
next();
};
};
11 changes: 7 additions & 4 deletions examples/express.ts → examples/custom-validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ const onComplete: OnComplete<DiskFile, UploadxResponse<OnCompleteBody>> = file =
return {
statusCode: 200,
message,
id: file.id,
headers: { ETag: file.id }
id: file.id
};
};

Expand All @@ -34,11 +33,15 @@ const storage = new DiskStorage({
validation: {
mime: { value: ['video/*'], response: [415, { message: 'video only' }] },
size: {
value: 500_000,
value: 500_000_000,
isValid(file) {
this.response = [
412,
{ message: `The file size(${file.size}) is larger than ${this.value as number} bytes` }
{
message: `File size(${file.size}) exceeds maximum permitted size of ${
this.value as number
} bytes`
}
];
return file.size <= this.value;
}
Expand Down
4 changes: 2 additions & 2 deletions examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
"private": true,
"scripts": {
"basic": "tsnd -r tsconfig-paths/register -r dotenv/config express-basic",
"express": "tsnd -r tsconfig-paths/register -r dotenv/config express",
"custom-validators": "tsnd -r tsconfig-paths/register -r dotenv/config custom-validators",
"gcs": "tsnd -r tsconfig-paths/register -r dotenv/config express-gcs",
"gcs:direct": "tsnd -r tsconfig-paths/register -r dotenv/config gcs-direct",
"node": "tsnd -r tsconfig-paths/register -r dotenv/config node-http-server",
"s3": "tsnd -r tsconfig-paths/register -r dotenv/config express-s3",
"tus": "tsnd -r tsconfig-paths/register -r dotenv/config express-tus",
"fastify": "tsnd -r tsconfig-paths/register -r dotenv/config fastify",
"processing": "tsnd -r tsconfig-paths/register -r dotenv/config processing",
"express-polling": "tsnd -r tsconfig-paths/register -r dotenv/config express-polling"
},
"dependencies": {
Expand Down
36 changes: 36 additions & 0 deletions examples/processing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { DiskFile, DiskStorage, Uploadx } from '@uploadx/core';
import { createHash } from 'crypto';
import * as express from 'express';
import { createReadStream } from 'fs';
import { join } from 'path';
import { completedRetryGuard } from './completed-retry-guard';

const app = express();

const uploadDirectory = 'upload';

const fileHash: (filePath: string) => Promise<string> = filePath =>
new Promise(resolve => {
const hash = createHash('sha256');
createReadStream(filePath)
.on('data', data => hash.update(data))
.on('end', () => resolve(hash.digest('hex')));
});

const storage = new DiskStorage({ directory: uploadDirectory });

const onComplete: express.RequestHandler = async (req, res, next) => {
const file = req.body as DiskFile;
try {
const sha256 = await fileHash(join(uploadDirectory, file.name));
res.finishUpload({ statusCode: 200, body: { id: file.id, sha256 } });
} catch (error) {
res.finishUpload({ statusCode: 422, body: { error: 'File hash calculation error' } });
}
};

const uploadx = new Uploadx({ storage });

app.all('/files', uploadx.upload, completedRetryGuard({ statusCode: 202 }, 60), onComplete);

app.listen(3002, () => console.log('listening on port:', 3002));