Skip to content

Commit

Permalink
feat: save video meta (#324)
Browse files Browse the repository at this point in the history
  • Loading branch information
annahassel committed Apr 17, 2024
1 parent 5890aba commit ef8bbcf
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 14 deletions.
4 changes: 4 additions & 0 deletions schemas/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,10 @@
"type": "number"
}
]
},
"^cfs:[0-9A-Fa-f]{32}$": {
"type": "string",
"maxLength": 8192
}
},
"allOf": [
Expand Down
34 changes: 27 additions & 7 deletions src/actions/cloudflare-stream.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { ActionTransport } = require('@microfleet/plugin-router');
const { HttpStatusError } = require('common-errors');
const stringify = require('safe-stable-stringify');

const { PROVIDER_CLOUDFLARE_MISSING_ERROR } = require('../constant');
const CloudflareStreamTransport = require('../providers/cloudflare-stream');
Expand Down Expand Up @@ -34,16 +35,35 @@ async function cloudflareWebhookAction(request) {
const message = parseBodyJson(body);
const { status, uid } = message;

if (status.state === STATUS_STATE_READY) {
await this.dispatch('finish', {
params: {
filename: CloudflareStreamTransport.filenameWithPrefix(uid),
},
});
} else {
if (status.state !== STATUS_STATE_READY) {
this.log.error({ message }, 'failed cloudflare-stream webhook');

return RESPONSE_OK;
}

const filename = CloudflareStreamTransport.filenameWithPrefix(uid);
const { uploadId, owner } = await this.dispatch('finish', {
params: {
filename,
await: true,
},
});

await this.dispatch('update', {
params: {
uploadId,
username: owner,
meta: {
[filename]: stringify({
duration: message.duration,
height: message.input.height,
size: message.size,
width: message.input.width,
}),
},
},
});

return RESPONSE_OK;
}

Expand Down
2 changes: 1 addition & 1 deletion src/actions/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,5 +273,5 @@ function initFileUpdate({ params }) {
return Promise.using(this.dlock.acquireLock(...keys), this, params, updateMeta);
}

initFileUpdate.transports = [ActionTransport.amqp];
initFileUpdate.transports = [ActionTransport.amqp, ActionTransport.internal];
module.exports = initFileUpdate;
20 changes: 14 additions & 6 deletions src/custom/cappasity-info-post.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,22 +369,30 @@ const GREEN_LIGHT_STATUSES = Object.setPrototypeOf({
[STATUS_FAILED]: true,
}, null);

const setCloudflareStreamPreview = async (service, file) => {
const setCloudflareStreamData = async (service, uploadData) => {
const cloudflareStream = service.providersByAlias['cloudflare-stream'];

if (cloudflareStream && file?.preview?.startsWith('cfs:')) {
if (file[FILES_PUBLIC_FIELD]) {
file.preview = await cloudflareStream.getThumbnailUrl(file.preview);
if (cloudflareStream && uploadData?.preview?.startsWith('cfs:')) {
if (uploadData[FILES_PUBLIC_FIELD]) {
uploadData.preview = await cloudflareStream.getThumbnailUrl(uploadData.preview);
} else {
file.preview = await cloudflareStream.getThumbnailUrlSigned(file.preview);
uploadData.preview = await cloudflareStream.getThumbnailUrlSigned(uploadData.preview);
}
}

for (const { filename } of uploadData.files) {
try {
uploadData[filename] = JSON.parse(uploadData[filename]);
} catch (error) {
service.log.error({ error, uploadData }, 'failed to parse cloudflare-stream meta');
}
}
};

// Actual code that populates .embed from predefined data
module.exports = async function getEmbeddedInfo(file) {
if (file.uploadType === UPLOAD_TYPE_CLOUDFLARE_STREAM) {
await setCloudflareStreamPreview(this, file);
await setCloudflareStreamData(this, file);

return file;
}
Expand Down
5 changes: 5 additions & 0 deletions test/suites/upload.cloudflare-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,11 @@ const assertInfo = async (response, uploadId, filename, signedURLs = true) => {
equal(response.file.files[0].type, 'video');
equal(response.file.files[0].filename, filename);

equal(response.file[filename].duration, 5.5);
equal(response.file[filename].height, 320);
equal(response.file[filename].size, 383631);
equal(response.file[filename].width, 560);

const { status, headers } = await fetch(response.file.preview);

equal(status, 200);
Expand Down

0 comments on commit ef8bbcf

Please sign in to comment.