asdass1 commited on
Commit
e1f0691
·
verified ·
1 Parent(s): b91ea0a

Upload 4 files

Browse files
Files changed (4) hide show
  1. Dockerfile +21 -0
  2. crc32.js +41 -0
  3. index_part.js +109 -0
  4. package.json +13 -0
Dockerfile ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Menggunakan image Node.js yang lebih stabil
2
+ FROM node:16-slim
3
+
4
+
5
+ # Tentukan direktori kerja di dalam container
6
+ WORKDIR /app
7
+
8
+ # Salin file package.json dan package-lock.json terlebih dahulu
9
+ COPY package*.json ./
10
+
11
+ # Install dependensi Node.js
12
+ RUN npm install
13
+
14
+ # Salin seluruh kode aplikasi ke dalam container
15
+ COPY . .
16
+
17
+ # Ekspose port 3000 yang akan digunakan aplikasi Express
18
+ EXPOSE 3000
19
+
20
+ # Tentukan perintah untuk menjalankan aplikasi Express.js
21
+ CMD ["npm", "start"]
crc32.js ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * 计算CRC32
3
+ * @param {ArrayBuffer} buffer - ArrayBuffer
4
+ */
5
+ export default buffer => {
6
+ buffer = new Uint8Array(buffer)
7
+ const module = {
8
+ polynom: 0xEDB88320,
9
+ initValue: -1,
10
+ bytes: 4,
11
+ table: []
12
+ }
13
+ for (let a = 0; a < 256; ++a) {
14
+ let b = a
15
+ for (let c = 0; c < 8; ++c) {
16
+ b = b & 1 ? module.polynom ^ (b >>> 1) : b >>> 1
17
+ }
18
+ module.table[a] = b >>> 0
19
+ }
20
+ let crc = module.initValue
21
+ let length = buffer.length
22
+ let table = module.table
23
+ for (let a = 0; a < 256; ++a) {
24
+ let b = a
25
+ for (let c = 0; c < 8; ++c) {
26
+ b = b & 1 ? module.polynom ^ (b >>> 1) : b >>> 1
27
+ }
28
+ module.table[a] = b >>> 0
29
+ }
30
+ for (let i = 0; i < length; ++i) {
31
+ crc = table[(crc ^ buffer[i]) & 0xFF] ^ (crc >>> 8)
32
+ }
33
+ crc ^= module.initValue
34
+ let HEX_CHARS = '0123456789abcdef'.split('');
35
+ let hex = '';
36
+ if (module.bytes > 2) {
37
+ hex += HEX_CHARS[(crc >> 28) & 0x0F] + HEX_CHARS[(crc >> 24) & 0x0F] + HEX_CHARS[(crc >> 20) & 0x0F] + HEX_CHARS[(crc >> 16) & 0x0F]
38
+ }
39
+ hex += HEX_CHARS[(crc >> 12) & 0x0F] + HEX_CHARS[(crc >> 8) & 0x0F] + HEX_CHARS[(crc >> 4) & 0x0F] + HEX_CHARS[crc & 0x0F]
40
+ return hex
41
+ }
index_part.js ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import crc32 from './crc32.js'
2
+ import http from 'axios'
3
+ import express from 'express'
4
+
5
+ const app = express()
6
+ const port = 7860
7
+
8
+ app.use(express.json())
9
+ app.use(express.urlencoded({ extended: true }))
10
+
11
+ function splitBuffer(buffer, n) {
12
+ const partSize = Math.ceil(buffer.length / n)
13
+ const parts = []
14
+
15
+ for (const i = 0; i < n; i++) {
16
+ const start = i * partSize
17
+ const end = start + partSize
18
+ parts.push(buffer.slice(start, end))
19
+ }
20
+
21
+ return parts
22
+ }
23
+
24
+ app.get('*', async (req, res) => {
25
+ try {
26
+ const file_url = req.path.replace(/^\//, '')
27
+ const file = await http({
28
+ method: 'GET',
29
+ url: file_url,
30
+ responseType: 'arraybuffer'
31
+ })
32
+ const files_list = splitBuffer(file, 5)
33
+ const upload_info = await http({
34
+ method: 'GET',
35
+ url: 'https://api.xs.cx/upload/sg'
36
+ })
37
+ const upload_part_info = await http({
38
+ method: 'POST',
39
+ url: upload_info.data.url,
40
+ params: {
41
+ uploadmode: 'part',
42
+ phase: 'init'
43
+ },
44
+ headers: {
45
+ 'authorization': upload_info.data.authorization
46
+ }
47
+ })
48
+ const uploadid = upload_part_info.data.data.uploadid
49
+ console.log(uploadid)
50
+ const upload_list = {}
51
+ await Promise.all(files_list.map(async (item, index) => {
52
+ const crc32_text = crc32(item)
53
+ let count = 1
54
+ while (count && count <= 5) {
55
+ const upload_result = await http({
56
+ method: 'POST',
57
+ url: upload_info.data.url,
58
+ params: {
59
+ uploadid: uploadid,
60
+ part_number: index + 1,
61
+ phase: 'transfer'
62
+ },
63
+ headers: {
64
+ 'content-crc32': crc32_text,
65
+ 'authorization': upload_info.data.authorization
66
+ },
67
+ data: item
68
+ })
69
+ if (upload_result.data.data.crc32 == crc32_text) {
70
+ count = 0
71
+ upload_list[index + 1] = crc32_text
72
+ } else {
73
+ count += 1
74
+ if (count > 5) {
75
+ throw Error
76
+ }
77
+ }
78
+ }
79
+ }))
80
+ const finish = Object.entries(upload_list).map(i => i.join(':')).join(',')
81
+ const upload_result = await http({
82
+ method: 'POST',
83
+ url: upload_info.data.url,
84
+ params: {
85
+ uploadid: uploadid,
86
+ uploadmode: 'part',
87
+ phase: 'finish'
88
+ },
89
+ headers: {
90
+ 'authorization': upload_info.data.authorization
91
+ },
92
+ data: finish
93
+ })
94
+ if (upload_result.data.code == 2000) {
95
+ res.send(JSON.stringify({
96
+ vid: upload_info.data.vid,
97
+ url: `https://sf16-sg-default.akamaized.net/obj/${upload_info.data.uri}`
98
+ }))
99
+ } else {
100
+ throw Error
101
+ }
102
+ } catch (e) {
103
+ res.send(e.toString())
104
+ }
105
+ })
106
+
107
+ app.listen(port, async () => {
108
+ console.log(`http://localhost:${port}`)
109
+ })
package.json ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "xx",
3
+ "version": "1.0.0",
4
+ "type": "module",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "start": "node index"
8
+ },
9
+ "dependencies": {
10
+ "axios": "^1.3.4",
11
+ "express": "^4.18.2"
12
+ }
13
+ }