Skip to content

Commit

Permalink
Fetch playback URL for imported asset using source URL (#1370)
Browse files Browse the repository at this point in the history
* api: Add index for source.url

* api: /playback lookup CID based on source URL

* api: make sure indexes work for oneOf fields

* api: Fix assets table test for index creation

Missing asset_source_url index in expected result

Co-authored-by: Victor Elias <victorgelias@gmail.com>
  • Loading branch information
yondonfu and victorges authored Oct 26, 2022
1 parent f6506aa commit 23ec9a7
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/api/src/controllers/asset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ describe("controllers/asset", () => {
"asset_playbackId",
"asset_playbackRecordingId",
"asset_sourceAssetId",
"asset_source_url",
"asset_storage_ipfs_cid",
"asset_storage_ipfs_nftMetadata_cid",
"asset_userId",
Expand Down
42 changes: 42 additions & 0 deletions packages/api/src/controllers/playback.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,48 @@ describe("controllers/playback", () => {
},
});
});

it("should return playback URL assets from CID based on source URL lookup", async () => {
const cid = "bafyfoobar";
await db.asset.update(asset.id, {
playbackRecordingId: "mock_recording_id_2",
source: {
type: "url",
url: "ipfs://" + cid,
},
status: {
phase: "ready",
updatedAt: 1234,
},
});
const res = await client.get(`/playback/${cid}`);
expect(res.status).toBe(200);
await expect(res.json()).resolves.toMatchObject({
type: "vod",
meta: {
source: [
{
hrn: "HLS (TS)",
type: "html5/application/vnd.apple.mpegurl",
url: `${ingest}/recordings/mock_recording_id_2/index.m3u8`,
},
],
},
});
});

it("should return 404 for CID based on source URL lookup if asset is not ready", async () => {
const cid = "bafyfoobar";
await db.asset.update(asset.id, {
playbackRecordingId: "mock_recording_id_2",
source: {
type: "url",
url: "ipfs://" + cid,
},
});
const res = await client.get(`/playback/${cid}`);
expect(res.status).toBe(404);
});
});

describe("for recordings", () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/api/src/controllers/playback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ const getAssetPlaybackUrl = async (
cid: boolean
) => {
const asset = cid
? await db.asset.getByIpfsCid(id)
? (await db.asset.getByIpfsCid(id)) ??
(await db.asset.getBySourceURL("ipfs://" + id))
: await db.asset.getByPlaybackId(id);
if (!asset || asset.deleted) {
return null;
Expand Down
1 change: 1 addition & 0 deletions packages/api/src/schema/schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,7 @@ components:
enum: [url]
url:
type: string
index: true
description: URL from which the asset was uploaded
- additionalProperties: false
required: [type]
Expand Down
17 changes: 17 additions & 0 deletions packages/api/src/store/asset-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,21 @@ export default class AssetTable extends Table<WithID<Asset>> {
}
return assets[0];
}

async getBySourceURL(url: string): Promise<WithID<Asset>> {
const query = [
sql`asset.data->'source'->>'type' = 'url'`,
sql`asset.data->'source'->>'url' = ${url}`,
sql`asset.data->>'deleted' IS NULL`,
sql`asset.data->'status'->>'phase' = 'ready'`,
];
const [assets] = await this.find(query, {
limit: 2,
order: "coalesce((asset.data->'createdAt')::bigint, 0) ASC",
});
if (!assets || assets.length < 1) {
return null;
}
return assets[0];
}
}
7 changes: 7 additions & 0 deletions packages/api/src/store/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,13 @@ export default class Table<T extends DBObject> {
// avoid creating indexes in production right now...
return;
}
if (prop.oneOf?.length) {
return Promise.all(
prop.oneOf.map((oneSchema) =>
this.ensureIndex(propName, oneSchema, parents)
)
);
}

if (!prop.index && !prop.unique) {
if (prop.properties && this.name === "asset") {
Expand Down

0 comments on commit 23ec9a7

Please sign in to comment.