-
Notifications
You must be signed in to change notification settings - Fork 0
/
tus-client-test.spec.js
158 lines (143 loc) · 5.42 KB
/
tus-client-test.spec.js
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
158
import * as tus from "tus-js-client";
import { createReadStream } from "node:fs";
import { Storage } from "./s3-utils";
import fsExtraPkg from "fs-extra";
const { stat, open, close, write, remove, readFile, pathExists } = fsExtraPkg;
describe(`Test TUS uploads using tus js client`, () => {
const endpoint = "http://localhost:8080/files";
const Bucket = "repository";
const storage = new Storage({
awsAccessKeyId: "root",
awsSecretAccessKey: "rootpass",
forcePathStyle: true,
endpoint: "http://minio:9000",
});
it(`Should be able to upload a 1MB file using the TUS JS client`, async () => {
const file = "./test-files/1mb.txt";
const filename = "1mb.txt";
const stream = createReadStream(file);
if (!(await pathExists(file))) {
console.log(
`Create this test file to run this test: 'dd if=/dev/zero of=api/test-files/1mb.txt bs=1024 count=1000'`
);
return;
}
const metadata = {
filename,
bucket: Bucket,
overwrite: true,
};
await TusUpload({ endpoint, stream, metadata, quiet: true });
// verify the uploaded file
const fileSize = (await stat(file)).size;
let exists = await storage.keyExists({ Bucket, Key: filename });
expect(exists).toBeTrue;
let s3FileStat = await storage.stat({ Bucket, Key: filename });
expect(s3FileStat.ContentLength).toEqual(fileSize);
let uploadedFile = await storage.downloadFile({
Bucket,
Key: filename,
});
let originalFile = await readFile(file);
expect(uploadedFile.toString()).toEqual(originalFile.toString());
await storage.removeObjects({
Bucket,
keys: [filename],
});
});
it(`Should be able to upload a 10MB file using the TUS JS client`, async () => {
const file = "./test-files/10mb.txt";
const filename = "10mb.txt";
const stream = createReadStream(file);
if (!(await pathExists(file))) {
console.log(
`Create this test file to run this test: 'dd if=/dev/zero of=api/test-files/10mb.txt bs=1024 count=10000'`
);
return;
}
const metadata = {
filename,
bucket: Bucket,
overwrite: true,
};
await TusUpload({ endpoint, stream, metadata, quiet: true });
// verify the uploaded file
const fileSize = (await stat(file)).size;
let exists = await storage.keyExists({ Bucket, Key: filename });
expect(exists).toBeTrue;
let s3FileStat = await storage.stat({ Bucket, Key: filename });
expect(s3FileStat.ContentLength).toEqual(fileSize);
let uploadedFile = await storage.downloadFile({
Bucket,
Key: filename,
});
let originalFile = await readFile(file);
expect(uploadedFile.toString()).toEqual(originalFile.toString());
await storage.removeObjects({
Bucket,
keys: [filename],
});
});
it(`Should be able to upload a 100MB file using the TUS JS client`, async () => {
const file = "./test-files/100mb.txt";
const filename = "100mb.txt";
const stream = createReadStream(file);
if (!(await pathExists(file))) {
console.log(
`Create this test file to run this test: 'dd if=/dev/zero of=api/test-files/100mb.txt bs=1024 count=100000'`
);
return;
}
const metadata = {
filename,
bucket: Bucket,
overwrite: true,
};
await TusUpload({ endpoint, stream, metadata, quiet: false });
// verify the uploaded file
const fileSize = (await stat(file)).size;
let exists = await storage.keyExists({ Bucket, Key: filename });
expect(exists).toBeTrue;
let s3FileStat = await storage.stat({ Bucket, Key: filename });
expect(s3FileStat.ContentLength).toEqual(fileSize);
let uploadedFile = await storage.downloadFile({
Bucket,
Key: filename,
});
let originalFile = await readFile(file);
expect(uploadedFile.toString()).toEqual(originalFile.toString());
await storage.removeObjects({
Bucket,
keys: [filename],
});
});
});
function TusUpload({ endpoint, stream, metadata, quiet = false }) {
return new Promise((resolve, reject) => {
const options = {
endpoint,
metadata,
headers: {
authorization: "Bearer secret",
"x-forwarded-host": "http://localhost:8080/files",
},
// chunk size = 128MB
chunkSize: 128 * 1024 * 1024,
onError(error) {
console.error("An error occurred:");
console.error(error);
reject();
},
onProgress(bytesUploaded, bytesTotal) {
const percentage = ((bytesUploaded / bytesTotal) * 100).toFixed(2);
if (!quiet) console.log(bytesUploaded, bytesTotal, `${percentage}%`);
},
onSuccess() {
if (!quiet) console.log("Upload finished:", upload.url);
resolve();
},
};
const upload = new tus.Upload(stream, options);
upload.start();
});
}