-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathfunction.js
More file actions
100 lines (88 loc) · 2.88 KB
/
Copy pathfunction.js
File metadata and controls
100 lines (88 loc) · 2.88 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
// Cloudflare Workers variant: replaces archiver (which relies on Node streams
// and prototype inheritance that breaks under Workers) with fflate, a pure-JS
// zip library that runs without any Node-specific APIs.
import * as fs from 'node:fs';
import * as path from 'node:path';
import { zipSync, strToU8 } from 'fflate';
import { v4 as uuidv4 } from 'uuid';
import { storage } from './storage';
let storage_handler = new storage();
function parseDirectory(directory) {
let size = 0;
function walkDir(dir) {
const files = fs.readdirSync(dir);
for (const file of files) {
const filepath = path.join(dir, file);
const stat = fs.statSync(filepath);
if (stat.isDirectory()) {
walkDir(filepath);
} else {
size += stat.size;
}
}
}
walkDir(directory);
return size;
}
function collectFiles(directory) {
const result = {};
function walkDir(dir, prefix) {
const files = fs.readdirSync(dir);
for (const file of files) {
const filepath = path.join(dir, file);
const relPath = prefix ? `${prefix}/${file}` : file;
const stat = fs.statSync(filepath);
if (stat.isDirectory()) {
walkDir(filepath, relPath);
} else {
result[relPath] = [fs.readFileSync(filepath), { level: 9 }];
}
}
}
walkDir(directory, '');
return result;
}
export const handler = async function(event) {
const bucket = event.bucket.bucket;
const input_prefix = event.bucket.input;
const output_prefix = event.bucket.output;
const key = event.object.key;
const download_path = path.join('/tmp', `${key}-${uuidv4()}`);
fs.mkdirSync(download_path, { recursive: true });
const s3_download_begin = Date.now();
await storage_handler.downloadDirectory(bucket, path.join(input_prefix, key), download_path);
const s3_download_stop = Date.now();
const size = parseDirectory(download_path);
const compress_begin = Date.now();
const archive_name = `${key}.zip`;
const archive_path = path.join('/tmp', archive_name);
const files = collectFiles(download_path);
const zipped = zipSync(files);
fs.writeFileSync(archive_path, zipped);
const compress_end = Date.now();
const archive_size = fs.statSync(archive_path).size;
const s3_upload_begin = Date.now();
const [key_name, uploadPromise] = storage_handler.upload(
bucket,
path.join(output_prefix, archive_name),
archive_path
);
await uploadPromise;
const s3_upload_stop = Date.now();
const download_time = (s3_download_stop - s3_download_begin) * 1000;
const upload_time = (s3_upload_stop - s3_upload_begin) * 1000;
const process_time = (compress_end - compress_begin) * 1000;
return {
result: {
bucket: bucket,
key: key_name
},
measurement: {
download_time: download_time,
download_size: size,
upload_time: upload_time,
upload_size: archive_size,
compute_time: process_time
}
};
};