Skip to content

Commit

Permalink
Set 'mp4' extension to in-process videos in pre-v4 API
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmz committed Feb 7, 2025
1 parent 2846ebe commit a2b2298
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 19 deletions.
8 changes: 8 additions & 0 deletions app/serializers/v2/attachment.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { API_VERSION_3 } from '../../api-versions';
import type { Attachment } from '../../models';
import { currentConfig } from '../../support/app-async-context';
import { setExtension } from '../../support/media-files/file-ext';
import type { MediaMetaData, MediaPreviews, MediaType } from '../../support/media-files/types';
import type { ISO8601DateTimeString, UUID } from '../../support/types';

Expand Down Expand Up @@ -118,6 +119,13 @@ function serializeAttachmentV2(att: Attachment): SerializedAttachmentV2 {

if (att.meta.inProgress) {
result.inProgress = true;

if (att.mediaType === 'video') {
// Optimistically set the URL as the URL of after-processed video
result.fileName = setExtension(att.fileName, 'mp4');
result.url = currentConfig().attachments.url + att.getRelFilePath('', 'mp4');
result.thumbnailUrl = result.url;
}
}

return result;
Expand Down
51 changes: 32 additions & 19 deletions test/functional/attachments.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { initJobProcessing } from '../../app/jobs';
import { eventNames, PubSubAdapter } from '../../app/support/PubSubAdapter';
import { getSingleton } from '../../app/app';
import { withModifiedConfig } from '../helpers/with-modified-config';
import { API_VERSION_4 } from '../../app/api-versions';

import {
createTestUser,
Expand Down Expand Up @@ -324,13 +325,13 @@ describe('Attachments', () => {
// At first, we should have a stub file
expect(resp, 'to satisfy', {
attachments: {
fileName: 'polyphon.tmp',
fileName: 'polyphon.mp4',
mediaType: 'general',
fileSize: '29',
createdAt: attObj.createdAt.getTime().toString(),
updatedAt: attObj.updatedAt.getTime().toString(),
url: attObj.getFileUrl('', 'tmp'),
thumbnailUrl: attObj.getFileUrl('', 'tmp'),
url: attObj.getFileUrl('', 'mp4'),
thumbnailUrl: attObj.getFileUrl('', 'mp4'),
imageSizes: expect.it('to equal', {}),
createdBy: luna.user.id,
postId: null,
Expand Down Expand Up @@ -691,17 +692,31 @@ describe('Attachments', () => {
const app = await getSingleton();
const port = process.env.PEPYATKA_SERVER_PORT || app.context.config.port;

lunaSubscribedToHerChannel = await Session.create(port, 'Luna subscribed to her channel');
lunaSubscribedToHerChannel = await Session.create(port, 'Luna subscribed to her channel', {
query: { apiVersion: API_VERSION_4 },
});
await lunaSubscribedToHerChannel.sendAsync('auth', { authToken: luna.authToken });

lunaSubscribedToPost = await Session.create(port, 'Luna subscribed to post');
lunaSubscribedToPost = await Session.create(port, 'Luna subscribed to post', {
query: { apiVersion: API_VERSION_4 },
});
await lunaSubscribedToPost.sendAsync('auth', { authToken: luna.authToken });

lunaSubscribedToAttachment = await Session.create(port, 'Luna subscribed to attachment');
lunaSubscribedToAttachment = await Session.create(port, 'Luna subscribed to attachment', {
query: { apiVersion: API_VERSION_4 },
});
await lunaSubscribedToAttachment.sendAsync('auth', { authToken: luna.authToken });

anonSubscribedToPost = await Session.create(port, 'Anonymous subscribed to post');
anonSubscribedToAttachment = await Session.create(port, 'Anonymous subscribed to attachment');
anonSubscribedToPost = await Session.create(port, 'Anonymous subscribed to post', {
query: { apiVersion: API_VERSION_4 },
});
anonSubscribedToAttachment = await Session.create(
port,
'Anonymous subscribed to attachment',
{
query: { apiVersion: API_VERSION_4 },
},
);
});
afterEach(() =>
[
Expand Down Expand Up @@ -735,9 +750,7 @@ describe('Attachments', () => {
lunaSubscribedToHerChannel.haveCollected(eventNames.ATTACHMENT_CREATED),
]);

expect(events, 'to satisfy', [
{ attachments: { id: attId, url: expect.it('to end with', '.tmp') } },
]);
expect(events, 'to satisfy', [{ attachments: { id: attId, meta: { inProgress: true } } }]);
clearCollected();
}

Expand All @@ -752,9 +765,9 @@ describe('Attachments', () => {
]);

expect(events, 'to satisfy', [
{ attachments: { id: attId, url: expect.it('to end with', '.tmp') } },
{ attachments: { id: attId, url: expect.it('to end with', '.tmp') } },
{ attachments: { id: attId, url: expect.it('to end with', '.tmp') } },
{ attachments: { id: attId, meta: { inProgress: true } } },
{ attachments: { id: attId, meta: { inProgress: true } } },
{ attachments: { id: attId, meta: { inProgress: true } } },
]);
}

Expand All @@ -778,17 +791,17 @@ describe('Attachments', () => {
]);

expect(events, 'to satisfy', [
{ attachments: { id: attId, url: expect.it('to end with', '.mp4') } },
{ attachments: { id: attId, previewTypes: ['image', 'video'] } },
{
posts: { id: post.id },
attachments: [{ id: attId, url: expect.it('to end with', '.mp4') }],
attachments: [{ id: attId, previewTypes: ['image', 'video'] }],
},
{ attachments: { id: attId, url: expect.it('to end with', '.mp4') } },
{ attachments: { id: attId, previewTypes: ['image', 'video'] } },
{
posts: { id: post.id },
attachments: [{ id: attId, url: expect.it('to end with', '.mp4') }],
attachments: [{ id: attId, previewTypes: ['image', 'video'] }],
},
{ attachments: { id: attId, url: expect.it('to end with', '.mp4') } },
{ attachments: { id: attId, previewTypes: ['image', 'video'] } },
]);
}
});
Expand Down

0 comments on commit a2b2298

Please sign in to comment.