@editframe/api 0.9.0-beta.3 → 0.10.0-beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -31,7 +31,7 @@ const FileProcessors = z.array(FileProcessor).refine(
31
31
  const MAX_FILE_SIZE = 1024 * 1024 * 1024; // 1GiB
32
32
 
33
33
  export const CreateUnprocessedFilePayload = z.object({
34
- id: z.string(),
34
+ md5: z.string(),
35
35
  filename: z.string(),
36
36
  processes: FileProcessors.optional(),
37
37
  byte_size: z.number().int().max(MAX_FILE_SIZE),
@@ -44,7 +44,9 @@ export const UpdateUnprocessedFilePayload = z.object({
44
44
  export interface CreateUnprocessedFileResult {
45
45
  byte_size: number;
46
46
  next_byte: number;
47
+ complete: boolean;
47
48
  id: string;
49
+ md5: string;
48
50
  processes: z.infer<typeof FileProcessors>;
49
51
  asset_id: string;
50
52
  }
@@ -52,7 +54,9 @@ export interface CreateUnprocessedFileResult {
52
54
  export interface UpdateUnprocessedFileResult {
53
55
  byte_size?: number;
54
56
  next_byte: number;
57
+ complete: boolean;
55
58
  id: string;
59
+ md5: string;
56
60
  processes: z.infer<typeof FileProcessors>;
57
61
  asset_id: string;
58
62
  }
@@ -130,67 +134,85 @@ export const uploadUnprocessedFile = async (
130
134
  log("Unprocessed file upload complete");
131
135
  };
132
136
 
133
- const buildBufferProcessor = (processor: z.infer<typeof FileProcessor>) => {
134
- return async (client: Client, buffer: Buffer, filename = "buffer") => {
135
- log(`Processing file buffer: ${processor}`, filename);
136
- const fileId = md5Buffer(buffer);
137
-
138
- log("File ID", fileId);
139
- log(`File size: ${buffer.byteLength} bytes`);
137
+ const processResource = async (
138
+ client: Client,
139
+ filename: string,
140
+ md5: string,
141
+ byteSize: number,
142
+ processor: z.infer<typeof FileProcessor>,
143
+ doUpload: (id: string) => Promise<void>,
144
+ ) => {
145
+ log("Processing", { filename, md5, byteSize, processor });
140
146
 
141
- await createUnprocessedFile(client, {
142
- id: fileId,
143
- processes: [],
144
- filename,
145
- byte_size: buffer.byteLength,
146
- });
147
+ const unprocessedFile = await createUnprocessedFile(client, {
148
+ md5: md5,
149
+ processes: [],
150
+ filename,
151
+ byte_size: byteSize,
152
+ });
147
153
 
148
- const readStream = new Readable({
149
- read() {
150
- readStream.push(buffer);
151
- readStream.push(null);
152
- },
153
- });
154
+ if (unprocessedFile.complete === false) {
155
+ await doUpload(unprocessedFile.id);
156
+ }
154
157
 
155
- await uploadUnprocessedFile(client, fileId, readStream, buffer.byteLength);
158
+ if (unprocessedFile.processes.includes(processor)) {
159
+ log("File already processed", unprocessedFile);
160
+ return unprocessedFile;
161
+ }
156
162
 
157
- const fileInformation = await updateUnprocessedFile(client, fileId, {
163
+ const fileInformation = await updateUnprocessedFile(
164
+ client,
165
+ unprocessedFile.id,
166
+ {
158
167
  processes: [processor],
159
- });
168
+ },
169
+ );
170
+ log("File processed", fileInformation);
171
+ return fileInformation;
172
+ };
160
173
 
161
- log("File processed", fileInformation);
162
- return fileInformation;
174
+ const buildBufferProcessor = (processor: z.infer<typeof FileProcessor>) => {
175
+ return async (client: Client, buffer: Buffer, filename = "buffer") => {
176
+ log(`Processing file buffer: ${processor}`, filename);
177
+ const md5 = md5Buffer(buffer);
178
+
179
+ return await processResource(
180
+ client,
181
+ filename,
182
+ md5,
183
+ buffer.byteLength,
184
+ processor,
185
+ async (id: string) => {
186
+ const readStream = new Readable({
187
+ read() {
188
+ readStream.push(buffer);
189
+ readStream.push(null);
190
+ },
191
+ });
192
+
193
+ await uploadUnprocessedFile(client, id, readStream, buffer.byteLength);
194
+ },
195
+ );
163
196
  };
164
197
  };
165
198
 
166
199
  const buildFileProcessor = (processor: z.infer<typeof FileProcessor>) => {
167
200
  return async (client: Client, filePath: string) => {
168
201
  log(`Processing file ${processor}`, filePath);
169
- const fileId = await md5FilePath(filePath);
170
-
171
- log("File ID", fileId);
172
- await createUnprocessedFile(client, {
173
- id: fileId,
174
- processes: [],
175
- filename: basename(filePath),
176
- byte_size: (await stat(filePath)).size,
177
- });
202
+ const md5 = await md5FilePath(filePath);
203
+ const byteSize = (await stat(filePath)).size;
178
204
 
179
- const readStream = createReadStream(filePath);
180
-
181
- await uploadUnprocessedFile(
205
+ return await processResource(
182
206
  client,
183
- fileId,
184
- readStream,
185
- (await stat(filePath)).size,
207
+ basename(filePath),
208
+ md5,
209
+ byteSize,
210
+ processor,
211
+ async (id: string) => {
212
+ const readStream = createReadStream(filePath);
213
+ return await uploadUnprocessedFile(client, id, readStream, byteSize);
214
+ },
186
215
  );
187
-
188
- const fileInformation = await updateUnprocessedFile(client, fileId, {
189
- processes: [processor],
190
- });
191
-
192
- log("File processed", fileInformation);
193
- return fileInformation;
194
216
  };
195
217
  };
196
218