|
8 | 8 | spawnWorker, |
9 | 9 | terminateWorker, |
10 | 10 | onMessage, |
11 | | - loadMedia, |
12 | 11 | send, |
| 12 | + fetchFile, |
| 13 | + fs, |
13 | 14 | } = require('./worker/node'); |
14 | 15 |
|
15 | 16 | let workerCounter = 0; |
@@ -58,57 +59,72 @@ module.exports = (_options = {}) => { |
58 | 59 | })) |
59 | 60 | ); |
60 | 61 |
|
61 | | - const write = async (path, data, jobId) => ( |
| 62 | + const syncfs = (populate, jobId) => ( |
62 | 63 | startJob(createJob({ |
63 | | - id: jobId, |
64 | | - action: 'write', |
65 | | - payload: { |
66 | | - path, |
67 | | - data: await loadMedia(data), |
68 | | - }, |
| 64 | + id: jobId, action: 'syncfs', payload: { populate }, |
69 | 65 | })) |
70 | 66 | ); |
71 | 67 |
|
72 | | - const writeText = async (path, text, jobId) => ( |
73 | | - startJob(createJob({ |
74 | | - id: jobId, |
75 | | - action: 'writeText', |
76 | | - payload: { |
77 | | - path, |
78 | | - text, |
79 | | - }, |
80 | | - })) |
81 | | - ); |
| 68 | + const write = async (path, data) => { |
| 69 | + await syncfs(); |
| 70 | + await fs.writeFile(path, await fetchFile(data)); |
| 71 | + await syncfs(true); |
| 72 | + return { |
| 73 | + path: `/data/${path}`, |
| 74 | + }; |
| 75 | + }; |
82 | 76 |
|
83 | | - const run = (args, jobId) => ( |
84 | | - startJob(createJob({ |
85 | | - id: jobId, action: 'run', payload: { args }, |
86 | | - })) |
87 | | - ); |
| 77 | + const writeText = async (path, text) => { |
| 78 | + await syncfs(true); |
| 79 | + await fs.writeFile(path, text); |
| 80 | + await syncfs(true); |
| 81 | + return { |
| 82 | + path: `/data/${path}`, |
| 83 | + }; |
| 84 | + }; |
88 | 85 |
|
89 | | - const transcode = (inputPath, outputPath, opts = '', jobId) => ( |
90 | | - run(`${opts} -i ${inputPath} ${outputPath}`, jobId) |
91 | | - ); |
| 86 | + const read = async (path, del = true) => { |
| 87 | + const data = await fs.readFile(path); |
| 88 | + if (del) { |
| 89 | + await fs.deleteFile(path); |
| 90 | + } |
| 91 | + return { |
| 92 | + data, |
| 93 | + }; |
| 94 | + }; |
92 | 95 |
|
93 | | - const trim = (inputPath, outputPath, from, to, opts = '', jobId) => ( |
94 | | - run(`${opts} -ss ${from} -i ${inputPath} -t ${to} -c copy ${outputPath}`, jobId) |
95 | | - ); |
| 96 | + const remove = async (path) => { |
| 97 | + await fs.deleteFile(path); |
| 98 | + return { |
| 99 | + path: `/data/${path}`, |
| 100 | + }; |
| 101 | + }; |
96 | 102 |
|
97 | | - const read = (path, jobId) => ( |
| 103 | + const run = (args, opts = {}, jobId) => ( |
98 | 104 | startJob(createJob({ |
99 | | - id: jobId, action: 'read', payload: { path }, |
| 105 | + id: jobId, action: 'run', payload: { args, options: opts }, |
100 | 106 | })) |
101 | 107 | ); |
102 | 108 |
|
103 | | - const remove = (path, jobId) => ( |
104 | | - startJob(createJob({ |
105 | | - id: jobId, action: 'remove', payload: { path }, |
106 | | - })) |
| 109 | + const transcode = (inputPath, outputPath, opts = '', del = true, jobId) => ( |
| 110 | + run( |
| 111 | + `${opts} -i /data/${inputPath} ${outputPath}`, |
| 112 | + { inputPath, outputPath, del }, |
| 113 | + jobId, |
| 114 | + ) |
| 115 | + ); |
| 116 | + |
| 117 | + const trim = (inputPath, outputPath, from, to, opts = '', del = true, jobId) => ( |
| 118 | + run( |
| 119 | + `${opts} -ss ${from} -i /data/${inputPath} -t ${to} -c copy ${outputPath}`, |
| 120 | + { inputPath, outputPath, del }, |
| 121 | + jobId, |
| 122 | + ) |
107 | 123 | ); |
108 | 124 |
|
109 | | - const mkdir = (path, jobId) => ( |
| 125 | + const ls = (path, jobId) => ( |
110 | 126 | startJob(createJob({ |
111 | | - id: jobId, action: 'mkdir', payload: { path }, |
| 127 | + id: jobId, action: 'ls', payload: { path }, |
112 | 128 | })) |
113 | 129 | ); |
114 | 130 |
|
@@ -151,14 +167,15 @@ module.exports = (_options = {}) => { |
151 | 167 | setResolve, |
152 | 168 | setReject, |
153 | 169 | load, |
| 170 | + syncfs, |
154 | 171 | write, |
155 | 172 | writeText, |
156 | | - transcode, |
157 | | - trim, |
158 | 173 | read, |
159 | 174 | remove, |
160 | | - mkdir, |
161 | 175 | run, |
| 176 | + transcode, |
| 177 | + trim, |
| 178 | + ls, |
162 | 179 | terminate, |
163 | 180 | }; |
164 | 181 | }; |
0 commit comments