@adonis-agora/media 0.23.0 → 0.25.0
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.
- package/CHANGELOG.md +20 -0
- package/README.md +12 -4
- package/dist/providers/media_provider.d.ts.map +1 -1
- package/dist/providers/media_provider.js +3 -0
- package/dist/providers/media_provider.js.map +1 -1
- package/dist/src/define_config.d.ts +6 -0
- package/dist/src/define_config.d.ts.map +1 -1
- package/dist/src/define_config.js.map +1 -1
- package/dist/src/index.d.ts +8 -4
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +3 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/media_access.d.ts +83 -0
- package/dist/src/media_access.d.ts.map +1 -1
- package/dist/src/media_access.js +177 -60
- package/dist/src/media_access.js.map +1 -1
- package/dist/src/media_library.d.ts +66 -0
- package/dist/src/media_library.d.ts.map +1 -1
- package/dist/src/media_library.js +147 -1
- package/dist/src/media_library.js.map +1 -1
- package/dist/src/media_manager.d.ts +2 -0
- package/dist/src/media_manager.d.ts.map +1 -1
- package/dist/src/media_manager.js +3 -0
- package/dist/src/media_manager.js.map +1 -1
- package/dist/src/streaming.d.ts +78 -0
- package/dist/src/streaming.d.ts.map +1 -0
- package/dist/src/streaming.js +143 -0
- package/dist/src/streaming.js.map +1 -0
- package/dist/src/transformers/dash.d.ts +70 -0
- package/dist/src/transformers/dash.d.ts.map +1 -0
- package/dist/src/transformers/dash.js +80 -0
- package/dist/src/transformers/dash.js.map +1 -0
- package/dist/src/transformers/factory.d.ts +10 -2
- package/dist/src/transformers/factory.d.ts.map +1 -1
- package/dist/src/transformers/factory.js +12 -2
- package/dist/src/transformers/factory.js.map +1 -1
- package/dist/src/transformers/ffmpeg.d.ts +38 -0
- package/dist/src/transformers/ffmpeg.d.ts.map +1 -1
- package/dist/src/transformers/ffmpeg.js +102 -10
- package/dist/src/transformers/ffmpeg.js.map +1 -1
- package/package.json +3 -3
- package/skills/media-stores-delivery/SKILL.md +10 -5
package/dist/src/media_access.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { adonisAuthIdentity, toAuthorizer, } from './access.js';
|
|
2
|
-
import {
|
|
2
|
+
import { deliveryQueryString } from './delivery_signing.js';
|
|
3
|
+
import { ConversionArtifactMissingError, ConversionNotDefinedError, DeliverySignatureError, ImageSizeNotAllowedError, MediaAccessDeniedError, MediaNotFoundError, NotAnImageError, TransformNotReadyError, } from './errors.js';
|
|
4
|
+
import { streamFormatOf } from './streaming.js';
|
|
3
5
|
/**
|
|
4
6
|
* Authorization for media, wired to the app's identity (`access.identity` — authkit, AdonisJS
|
|
5
7
|
* auth or your own) and the collections' `access` policies. `media.access` on the manager.
|
|
@@ -62,6 +64,168 @@ export class MediaAccess {
|
|
|
62
64
|
await this.authorize(ctx, 'read', record, authorize);
|
|
63
65
|
return this.#library.image(id, imageOptions);
|
|
64
66
|
}
|
|
67
|
+
/**
|
|
68
|
+
* `media.library.stream(id)` for the current user: checks read access first (the collection's
|
|
69
|
+
* `access.read`, or `authorize`), then returns the HLS/DASH manifest URLs a player needs — for
|
|
70
|
+
* `useMediaStream` from `@adonis-agora/media-react`.
|
|
71
|
+
*
|
|
72
|
+
* ```ts
|
|
73
|
+
* return inertia.render('lessons/show', { video: await media.access.stream(ctx, lesson.videoId) })
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
async stream(ctx, id, options = {}) {
|
|
77
|
+
const { authorize, ...streamOptions } = options;
|
|
78
|
+
const record = await this.#library.find(id);
|
|
79
|
+
if (!record)
|
|
80
|
+
throw new MediaNotFoundError(id);
|
|
81
|
+
await this.authorize(ctx, 'read', record, authorize);
|
|
82
|
+
return this.#library.stream(id, streamOptions);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Authorize and answer one request for a file of a streaming package (HLS or DASH) — the core
|
|
86
|
+
* of {@link streamRoute}, for a controller that finds the record its own way.
|
|
87
|
+
*
|
|
88
|
+
* A request carrying a valid package signature (`?exp=&sig=`, from `media.library.stream()`
|
|
89
|
+
* with `delivery.signing`) is served without asking the app again; anything else is authorized
|
|
90
|
+
* like a read. A manifest is answered as text, with the signature carried onto its references
|
|
91
|
+
* (a fresh one when the request was authorized by session), so the player's segment requests
|
|
92
|
+
* stay authorized. Segments are redirected to storage or streamed, per `mode`.
|
|
93
|
+
*/
|
|
94
|
+
async serveStream(ctx, target, conversion, file, options = {}) {
|
|
95
|
+
const { request, response } = ctx;
|
|
96
|
+
const cacheControl = options.cacheControl ?? 'private, max-age=60';
|
|
97
|
+
try {
|
|
98
|
+
const record = typeof target === 'string' ? await this.#library.find(target) : (target ?? null);
|
|
99
|
+
if (!record)
|
|
100
|
+
throw new MediaNotFoundError(typeof target === 'string' ? target : '');
|
|
101
|
+
const qs = request.qs();
|
|
102
|
+
const expires = stringParam(qs.exp);
|
|
103
|
+
const signature = stringParam(qs.sig);
|
|
104
|
+
const trusted = this.#library.checkDeliverySignature({ mediaId: record.id, conversion, expires, signature }, { required: false });
|
|
105
|
+
if (!trusted)
|
|
106
|
+
await this.authorize(ctx, 'read', record, options.authorize);
|
|
107
|
+
// Only a manifest needs the signature its references will carry.
|
|
108
|
+
const query = !streamFormatOf(file)
|
|
109
|
+
? ''
|
|
110
|
+
: trusted
|
|
111
|
+
? deliveryQueryString({ expires: Number(expires), signature }).slice(1)
|
|
112
|
+
: this.#library.streamQuery(record, conversion, options.expiresIn);
|
|
113
|
+
const result = await this.#library.streamFile(record, conversion, file, {
|
|
114
|
+
query,
|
|
115
|
+
mode: options.mode,
|
|
116
|
+
signedTtlSeconds: options.signedTtlSeconds,
|
|
117
|
+
});
|
|
118
|
+
if (result.kind === 'manifest') {
|
|
119
|
+
response.header('cache-control', cacheControl);
|
|
120
|
+
response.header('content-type', result.contentType);
|
|
121
|
+
return response.send(result.content);
|
|
122
|
+
}
|
|
123
|
+
if (result.kind === 'redirect') {
|
|
124
|
+
response.header('cache-control', cacheControl);
|
|
125
|
+
return response.redirect(result.url);
|
|
126
|
+
}
|
|
127
|
+
response.header('cache-control', options.segmentCacheControl ?? 'private, max-age=86400');
|
|
128
|
+
response.header('content-type', result.mimeType);
|
|
129
|
+
if (result.size !== undefined)
|
|
130
|
+
response.header('content-length', String(result.size));
|
|
131
|
+
return response.stream(result.stream);
|
|
132
|
+
}
|
|
133
|
+
catch (error) {
|
|
134
|
+
return answerError(error, response);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* A ready AdonisJS route handler for HLS/DASH packages — the read side of `transformers.hls()`
|
|
139
|
+
* and `transformers.dash()`:
|
|
140
|
+
*
|
|
141
|
+
* ```ts
|
|
142
|
+
* const serveStream = media.access.streamRoute()
|
|
143
|
+
* router.get('/media/:id/stream/:conversion/*', serveStream)
|
|
144
|
+
* ```
|
|
145
|
+
*
|
|
146
|
+
* The URL mirrors the package: `/media/<id>/stream/<conversion>/<file>`, so the relative
|
|
147
|
+
* references a manifest stores resolve back to this same route. See {@link serveStream}.
|
|
148
|
+
*/
|
|
149
|
+
streamRoute(options = {}) {
|
|
150
|
+
return (ctx) => {
|
|
151
|
+
const wildcard = ctx.params['*'];
|
|
152
|
+
const file = Array.isArray(wildcard)
|
|
153
|
+
? wildcard.map(String).join('/')
|
|
154
|
+
: String(ctx.params.file ?? wildcard ?? '');
|
|
155
|
+
return this.serveStream(ctx, String(ctx.params.id ?? ''), String(ctx.params.conversion ?? ''), file, options);
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Authorize and answer one media read — the core of {@link deliveryRoute}, for a controller that
|
|
160
|
+
* finds the record its own way (a stable `/profiles/:id/photo` URL, a record looked up by its
|
|
161
|
+
* owner):
|
|
162
|
+
*
|
|
163
|
+
* ```ts
|
|
164
|
+
* async show(ctx) {
|
|
165
|
+
* const [avatar] = await media.library.list('User', ctx.params.id, 'avatar')
|
|
166
|
+
* return media.access.serve(ctx, avatar, { authorize: canSeeProfile })
|
|
167
|
+
* }
|
|
168
|
+
* ```
|
|
169
|
+
*
|
|
170
|
+
* `target` is the record, its id, or `null`/`undefined` (answered 404). Everything else is as in
|
|
171
|
+
* {@link deliveryRoute}: the read is authorized unless the URL carries a valid signature, the query
|
|
172
|
+
* (`?conversion`, `?w`, `?q`, `?exp`, `?sig`) and `Accept` are honoured, the file is redirected or
|
|
173
|
+
* streamed with its headers, and the library's errors are answered as HTTP.
|
|
174
|
+
*/
|
|
175
|
+
async serve(ctx, target, options = {}) {
|
|
176
|
+
const { request, response } = ctx;
|
|
177
|
+
const cacheControl = options.cacheControl ?? 'private, max-age=60';
|
|
178
|
+
try {
|
|
179
|
+
const record = typeof target === 'string' ? await this.#library.find(target) : (target ?? null);
|
|
180
|
+
if (!record)
|
|
181
|
+
throw new MediaNotFoundError(typeof target === 'string' ? target : '');
|
|
182
|
+
const id = record.id;
|
|
183
|
+
const qs = request.qs();
|
|
184
|
+
const signed = {
|
|
185
|
+
mediaId: id,
|
|
186
|
+
conversion: stringParam(qs.conversion),
|
|
187
|
+
width: stringParam(qs.w),
|
|
188
|
+
quality: stringParam(qs.q),
|
|
189
|
+
expires: stringParam(qs.exp),
|
|
190
|
+
signature: stringParam(qs.sig),
|
|
191
|
+
};
|
|
192
|
+
// A valid signature is a prior authorization; an invalid or expired one is refused
|
|
193
|
+
// (DeliverySignatureError). Unsigned requests are this caller's to authorize.
|
|
194
|
+
const trusted = this.#library.checkDeliverySignature(signed, { required: false });
|
|
195
|
+
if (!trusted)
|
|
196
|
+
await this.authorize(ctx, 'read', record, options.authorize);
|
|
197
|
+
const result = await this.#library.deliver(id, {
|
|
198
|
+
...(signed.conversion !== undefined ? { conversion: signed.conversion } : {}),
|
|
199
|
+
...(signed.width !== undefined && signed.width !== ''
|
|
200
|
+
? {
|
|
201
|
+
width: signed.width,
|
|
202
|
+
quality: signed.quality,
|
|
203
|
+
accept: request.header('accept') ?? undefined,
|
|
204
|
+
}
|
|
205
|
+
: {}),
|
|
206
|
+
...(trusted ? { trusted: true } : {}),
|
|
207
|
+
...(options.mode !== undefined ? { mode: options.mode } : {}),
|
|
208
|
+
...(options.signedTtlSeconds !== undefined
|
|
209
|
+
? { signedTtlSeconds: options.signedTtlSeconds }
|
|
210
|
+
: {}),
|
|
211
|
+
});
|
|
212
|
+
response.header('cache-control', cacheControl);
|
|
213
|
+
if (result.vary)
|
|
214
|
+
response.header('vary', result.vary);
|
|
215
|
+
if (result.kind === 'redirect')
|
|
216
|
+
return response.redirect(result.url);
|
|
217
|
+
response.header('content-type', result.mimeType);
|
|
218
|
+
if (result.size !== undefined)
|
|
219
|
+
response.header('content-length', String(result.size));
|
|
220
|
+
if (result.fileName) {
|
|
221
|
+
response.header('content-disposition', inlineDisposition(result.fileName));
|
|
222
|
+
}
|
|
223
|
+
return response.stream(result.stream);
|
|
224
|
+
}
|
|
225
|
+
catch (error) {
|
|
226
|
+
return answerError(error, response);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
65
229
|
/**
|
|
66
230
|
* A ready AdonisJS route handler that serves a media record — the delivery route every app was
|
|
67
231
|
* hand-writing:
|
|
@@ -83,67 +247,18 @@ export class MediaAccess {
|
|
|
83
247
|
*/
|
|
84
248
|
deliveryRoute(options = {}) {
|
|
85
249
|
const param = options.param ?? 'id';
|
|
86
|
-
|
|
87
|
-
return async (ctx) => {
|
|
88
|
-
const { request, response } = ctx;
|
|
89
|
-
try {
|
|
90
|
-
const id = String(ctx.params[param] ?? '');
|
|
91
|
-
const record = await this.#library.find(id);
|
|
92
|
-
if (!record)
|
|
93
|
-
throw new MediaNotFoundError(id);
|
|
94
|
-
const qs = request.qs();
|
|
95
|
-
const signed = {
|
|
96
|
-
mediaId: id,
|
|
97
|
-
conversion: stringParam(qs.conversion),
|
|
98
|
-
width: stringParam(qs.w),
|
|
99
|
-
quality: stringParam(qs.q),
|
|
100
|
-
expires: stringParam(qs.exp),
|
|
101
|
-
signature: stringParam(qs.sig),
|
|
102
|
-
};
|
|
103
|
-
// A valid signature is a prior authorization; an invalid or expired one is refused
|
|
104
|
-
// (DeliverySignatureError). Unsigned requests are this route's to authorize.
|
|
105
|
-
const trusted = this.#library.checkDeliverySignature(signed, { required: false });
|
|
106
|
-
if (!trusted)
|
|
107
|
-
await this.authorize(ctx, 'read', record, options.authorize);
|
|
108
|
-
const result = await this.#library.deliver(id, {
|
|
109
|
-
...(signed.conversion !== undefined ? { conversion: signed.conversion } : {}),
|
|
110
|
-
...(signed.width !== undefined && signed.width !== ''
|
|
111
|
-
? {
|
|
112
|
-
width: signed.width,
|
|
113
|
-
quality: signed.quality,
|
|
114
|
-
accept: request.header('accept') ?? undefined,
|
|
115
|
-
}
|
|
116
|
-
: {}),
|
|
117
|
-
...(trusted ? { trusted: true } : {}),
|
|
118
|
-
...(options.mode !== undefined ? { mode: options.mode } : {}),
|
|
119
|
-
...(options.signedTtlSeconds !== undefined
|
|
120
|
-
? { signedTtlSeconds: options.signedTtlSeconds }
|
|
121
|
-
: {}),
|
|
122
|
-
});
|
|
123
|
-
response.header('cache-control', cacheControl);
|
|
124
|
-
if (result.vary)
|
|
125
|
-
response.header('vary', result.vary);
|
|
126
|
-
if (result.kind === 'redirect')
|
|
127
|
-
return response.redirect(result.url);
|
|
128
|
-
response.header('content-type', result.mimeType);
|
|
129
|
-
if (result.size !== undefined)
|
|
130
|
-
response.header('content-length', String(result.size));
|
|
131
|
-
if (result.fileName) {
|
|
132
|
-
response.header('content-disposition', inlineDisposition(result.fileName));
|
|
133
|
-
}
|
|
134
|
-
return response.stream(result.stream);
|
|
135
|
-
}
|
|
136
|
-
catch (error) {
|
|
137
|
-
const status = httpStatusOf(error);
|
|
138
|
-
if (status === undefined)
|
|
139
|
-
throw error;
|
|
140
|
-
const { code, message } = error;
|
|
141
|
-
const replied = response.status(status);
|
|
142
|
-
return replied.json?.({ code, message });
|
|
143
|
-
}
|
|
144
|
-
};
|
|
250
|
+
return (ctx) => this.serve(ctx, String(ctx.params[param] ?? ''), options);
|
|
145
251
|
}
|
|
146
252
|
}
|
|
253
|
+
/** Answer a library error as `{ code, message }` with its HTTP status; rethrow anything else. */
|
|
254
|
+
function answerError(error, response) {
|
|
255
|
+
const status = httpStatusOf(error);
|
|
256
|
+
if (status === undefined)
|
|
257
|
+
throw error;
|
|
258
|
+
const { code, message } = error;
|
|
259
|
+
const replied = response.status(status);
|
|
260
|
+
return replied.json?.({ code, message });
|
|
261
|
+
}
|
|
147
262
|
/** The HTTP status for an error the delivery route answers itself; `undefined` ⇒ rethrow. */
|
|
148
263
|
function httpStatusOf(error) {
|
|
149
264
|
if (error instanceof MediaAccessDeniedError)
|
|
@@ -156,6 +271,8 @@ function httpStatusOf(error) {
|
|
|
156
271
|
return 404;
|
|
157
272
|
if (error instanceof TransformNotReadyError)
|
|
158
273
|
return 404;
|
|
274
|
+
if (error instanceof ConversionArtifactMissingError)
|
|
275
|
+
return 404;
|
|
159
276
|
if (error instanceof ImageSizeNotAllowedError)
|
|
160
277
|
return 400;
|
|
161
278
|
if (error instanceof NotAnImageError)
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"media_access.js","sourceRoot":"","sources":["../../src/media_access.ts"],"names":[],"mappings":"AACA,OAAO,EACL,kBAAkB,EAKlB,YAAY,GACb,MAAM,aAAa,CAAC;AAErB,OAAO,EACL,yBAAyB,EACzB,sBAAsB,EACtB,wBAAwB,EACxB,sBAAsB,EACtB,kBAAkB,EAClB,eAAe,EACf,sBAAsB,GACvB,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"media_access.js","sourceRoot":"","sources":["../../src/media_access.ts"],"names":[],"mappings":"AACA,OAAO,EACL,kBAAkB,EAKlB,YAAY,GACb,MAAM,aAAa,CAAC;AAErB,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,EACL,8BAA8B,EAC9B,yBAAyB,EACzB,sBAAsB,EACtB,wBAAwB,EACxB,sBAAsB,EACtB,kBAAkB,EAClB,eAAe,EACf,sBAAsB,GACvB,MA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@@ -7,6 +7,7 @@ import type { MediaRecord } from './media_record.js';
|
|
|
7
7
|
import type { MediaStore } from './media_store.js';
|
|
8
8
|
import { type ResponsiveImage, type ResponsiveImageOptions } from './responsive_image.js';
|
|
9
9
|
import type { StorageManager } from './storage_manager.js';
|
|
10
|
+
import { type MediaStream } from './streaming.js';
|
|
10
11
|
import type { SignedUrlOptions } from './types.js';
|
|
11
12
|
export interface MediaLibraryOptions {
|
|
12
13
|
storage: StorageManager;
|
|
@@ -30,7 +31,19 @@ export interface MediaLibraryOptions {
|
|
|
30
31
|
deliverySigning?: DeliverySigningConfig;
|
|
31
32
|
/** Route the app mounts `MediaDeliveryHandler` on, with `:id` (`delivery.path`). Default `/media/:id`. */
|
|
32
33
|
deliveryPath?: string;
|
|
34
|
+
/**
|
|
35
|
+
* Route the app mounts `media.access.streamRoute()` on, with `:id` and `:conversion`, WITHOUT
|
|
36
|
+
* the trailing file part (`delivery.streamPath`). Default `/media/:id/stream/:conversion`.
|
|
37
|
+
*/
|
|
38
|
+
deliveryStreamPath?: string;
|
|
33
39
|
}
|
|
40
|
+
/** What serving one file of a streaming package yields: a manifest's text, or the usual delivery. */
|
|
41
|
+
export type StreamFileResult = {
|
|
42
|
+
kind: 'manifest';
|
|
43
|
+
content: string;
|
|
44
|
+
contentType: string;
|
|
45
|
+
fileName: string;
|
|
46
|
+
} | DeliveryResult;
|
|
34
47
|
export interface AttachInput {
|
|
35
48
|
ownerType: string;
|
|
36
49
|
/** Owner primary key; a numeric Lucid id is accepted and coerced to a string internally. */
|
|
@@ -150,6 +163,7 @@ export declare class MediaLibrary {
|
|
|
150
163
|
private readonly deliveryImages;
|
|
151
164
|
private readonly deliverySigning;
|
|
152
165
|
private readonly deliveryPath;
|
|
166
|
+
private readonly deliveryStreamPath;
|
|
153
167
|
constructor(options: MediaLibraryOptions);
|
|
154
168
|
private emit;
|
|
155
169
|
attach(input: AttachInput): Promise<MediaRecord>;
|
|
@@ -283,6 +297,58 @@ export declare class MediaLibrary {
|
|
|
283
297
|
/** Seconds; overrides `delivery.signing.expiresIn`. */
|
|
284
298
|
expiresIn?: number;
|
|
285
299
|
}): string;
|
|
300
|
+
/**
|
|
301
|
+
* What a player needs to stream a video: the URLs of every HLS/DASH manifest the record's
|
|
302
|
+
* conversions provide (`transformers.hls()`, `transformers.dash()`), HLS first, through the
|
|
303
|
+
* stream route (`delivery.streamPath`, served by `media.access.streamRoute()`). Pass the result
|
|
304
|
+
* to `useMediaStream` from `@adonis-agora/media-react` as is.
|
|
305
|
+
*
|
|
306
|
+
* With `delivery.signing`, the URLs are signed for the whole package: the route then serves the
|
|
307
|
+
* manifest and every segment it leads to without asking the app again, until they expire. The
|
|
308
|
+
* lifetime is `expiresIn`, else `delivery.signing.expiresIn` — but never shorter than the video
|
|
309
|
+
* plus an hour, so a player does not lose access mid-video.
|
|
310
|
+
*
|
|
311
|
+
* Like {@link image}, this answers "how", not "who": authorize the caller first — or use
|
|
312
|
+
* `media.access.stream(ctx, id)`, which does.
|
|
313
|
+
*
|
|
314
|
+
* Throws {@link TransformNotReadyError} when no streamable conversion exists yet.
|
|
315
|
+
*/
|
|
316
|
+
stream(id: string, options?: {
|
|
317
|
+
/** Which conversions to offer, in order. Default: every streamable one. */
|
|
318
|
+
conversions?: readonly string[];
|
|
319
|
+
/** Signed-URL lifetime in seconds (with `delivery.signing`). */
|
|
320
|
+
expiresIn?: number;
|
|
321
|
+
}): Promise<MediaStream>;
|
|
322
|
+
/**
|
|
323
|
+
* URL of one file of a streaming package on the stream route — signed for the whole package
|
|
324
|
+
* with `delivery.signing`. {@link stream} builds the entry URLs with it.
|
|
325
|
+
*/
|
|
326
|
+
streamUrl(record: MediaRecord, conversion: string, file: string, options?: {
|
|
327
|
+
expiresIn?: number;
|
|
328
|
+
}): string;
|
|
329
|
+
/**
|
|
330
|
+
* The `exp=…&sig=…` that authorizes every file of one streaming package (no `?`), or `''`
|
|
331
|
+
* without `delivery.signing`. The signature covers the record and the conversion — not a file —
|
|
332
|
+
* so it can ride along on every reference a manifest makes.
|
|
333
|
+
*/
|
|
334
|
+
streamQuery(record: MediaRecord, conversion: string, expiresIn?: number): string;
|
|
335
|
+
/**
|
|
336
|
+
* Serve one file of a record's streaming package — the "how" of `media.access.streamRoute()`
|
|
337
|
+
* (authorize first). `file` is checked against the package's persisted artifact list, so a
|
|
338
|
+
* request can never reach outside it.
|
|
339
|
+
*
|
|
340
|
+
* - A **manifest** (`.m3u8`/`.mpd`) comes back as text. Its references are relative and resolve
|
|
341
|
+
* to the same route, so it is served as stored — plus `query` (a package signature) on every
|
|
342
|
+
* relative reference when given, which is how segment requests inherit the authorization.
|
|
343
|
+
* - Anything else (segments, init sections) is delivered per `mode` (default `delivery.mode`):
|
|
344
|
+
* a redirect to the object (signed URLs stay short-lived — they are minted per segment
|
|
345
|
+
* request), or a stream through the app.
|
|
346
|
+
*/
|
|
347
|
+
streamFile(record: MediaRecord, conversion: string, file: string, options?: {
|
|
348
|
+
query?: string | undefined;
|
|
349
|
+
mode?: DeliveryMode | undefined;
|
|
350
|
+
signedTtlSeconds?: number | undefined;
|
|
351
|
+
}): Promise<StreamFileResult>;
|
|
286
352
|
/**
|
|
287
353
|
* Check a delivery request against `delivery.signing`: `true` when it is signed and valid (serve
|
|
288
354
|
* any size it names), `false` when signing is off or the request is unsigned and that is allowed.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"media_library.d.ts","sourceRoot":"","sources":["../../src/media_library.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAG5C,OAAO,EAIL,KAAK,YAAY,EACjB,KAAK,cAAc,EACnB,KAAK,mBAAmB,EAEzB,MAAM,eAAe,CAAC;AACvB,OAAO,EAEL,KAAK,qBAAqB,EAK3B,MAAM,uBAAuB,CAAC;AAqB/B,OAAO,EAEL,KAAK,UAAU,EAEf,KAAK,cAAc,EAEpB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,KAA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,EAAE,YAAY,GAAG,SAAS,CAAC;QAChC,gBAAgB,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;KAClC,GACL,OAAO,CAAC,gBAAgB,CAAC,CA8C3B;IAED;;;;;OAKG;IACH,sBAAsB,CACpB,OAAO,EAAE;QACP,OAAO,EAAE,MAAM,CAAC;QAChB,UAAU,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;QAChC,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAAC;QACpC,OAAO,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAAC;QACtC,OAAO,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAAC;QACtC,SAAS,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;KAChC,EACD,OAAO,GAAE;QACP,+FAA+F;QAC/F,QAAQ,CAAC,EAAE,OAAO,CAAC;KACf,GACL,OAAO,CAOT;IAED;;;;;;;;;;;;;;;OAeG;IACG,OAAO,CAAC,EAAE,EAAE,MAAM,EAAE,OAAO,GAAE,cAAmB,GAAG,OAAO,CAAC,cAAc,CAAC,CAwC/E;IAED;;;;;;OAMG;IACG,SAAS,CACb,EAAE,EAAE,MAAM,EACV,SAAS,EAAE,MAAM,GAAG,MAAM,EAC1B,OAAO,GAAE,qBAA0B,GAClC,OAAO,CAAC,MAAM,CAAC,CAMjB;CACF"}
|
|
@@ -11,6 +11,7 @@ import { focalPointOf, presetCrops, } from './image_processor.js';
|
|
|
11
11
|
import { MediaCollectionRegistry } from './media_collection.js';
|
|
12
12
|
import { resolveDeclaredMimeType } from './mime_type.js';
|
|
13
13
|
import { BROWSER_IMAGE_TYPES, imageCandidates, } from './responsive_image.js';
|
|
14
|
+
import { DASH_MIME_TYPE, HLS_MIME_TYPE, signManifest, streamArtifactContentType, streamEntries, streamFormatOf, } from './streaming.js';
|
|
14
15
|
import { placeholderOf } from './transformers/placeholder.js';
|
|
15
16
|
export class MediaLibrary {
|
|
16
17
|
/**
|
|
@@ -36,6 +37,7 @@ export class MediaLibrary {
|
|
|
36
37
|
deliveryImages;
|
|
37
38
|
deliverySigning;
|
|
38
39
|
deliveryPath;
|
|
40
|
+
deliveryStreamPath;
|
|
39
41
|
constructor(options) {
|
|
40
42
|
this.storage = options.storage;
|
|
41
43
|
this.store = options.store;
|
|
@@ -54,6 +56,7 @@ export class MediaLibrary {
|
|
|
54
56
|
};
|
|
55
57
|
this.deliverySigning = options.deliverySigning;
|
|
56
58
|
this.deliveryPath = options.deliveryPath ?? '/media/:id';
|
|
59
|
+
this.deliveryStreamPath = options.deliveryStreamPath ?? '/media/:id/stream/:conversion';
|
|
57
60
|
}
|
|
58
61
|
emit(event, payload) {
|
|
59
62
|
if (this.emitDiagnostics)
|
|
@@ -868,6 +871,135 @@ export class MediaLibrary {
|
|
|
868
871
|
});
|
|
869
872
|
return path + deliveryQueryString({ conversion, width, quality, expires, signature });
|
|
870
873
|
}
|
|
874
|
+
/**
|
|
875
|
+
* What a player needs to stream a video: the URLs of every HLS/DASH manifest the record's
|
|
876
|
+
* conversions provide (`transformers.hls()`, `transformers.dash()`), HLS first, through the
|
|
877
|
+
* stream route (`delivery.streamPath`, served by `media.access.streamRoute()`). Pass the result
|
|
878
|
+
* to `useMediaStream` from `@adonis-agora/media-react` as is.
|
|
879
|
+
*
|
|
880
|
+
* With `delivery.signing`, the URLs are signed for the whole package: the route then serves the
|
|
881
|
+
* manifest and every segment it leads to without asking the app again, until they expire. The
|
|
882
|
+
* lifetime is `expiresIn`, else `delivery.signing.expiresIn` — but never shorter than the video
|
|
883
|
+
* plus an hour, so a player does not lose access mid-video.
|
|
884
|
+
*
|
|
885
|
+
* Like {@link image}, this answers "how", not "who": authorize the caller first — or use
|
|
886
|
+
* `media.access.stream(ctx, id)`, which does.
|
|
887
|
+
*
|
|
888
|
+
* Throws {@link TransformNotReadyError} when no streamable conversion exists yet.
|
|
889
|
+
*/
|
|
890
|
+
async stream(id, options = {}) {
|
|
891
|
+
const record = await this.store.find(id);
|
|
892
|
+
if (!record)
|
|
893
|
+
throw new MediaNotFoundError(id);
|
|
894
|
+
const entries = streamEntries(record, options.conversions);
|
|
895
|
+
const first = entries[0];
|
|
896
|
+
if (!first)
|
|
897
|
+
throw new TransformNotReadyError(id, options.conversions?.join(', ') ?? 'hls/dash');
|
|
898
|
+
const sources = entries.map((entry) => ({
|
|
899
|
+
src: this.streamUrl(record, entry.conversion, entry.file, options),
|
|
900
|
+
format: entry.format,
|
|
901
|
+
type: entry.format === 'hls' ? HLS_MIME_TYPE : DASH_MIME_TYPE,
|
|
902
|
+
conversion: entry.conversion,
|
|
903
|
+
}));
|
|
904
|
+
const meta = record.conversions[first.conversion]?.meta ?? {};
|
|
905
|
+
const numeric = (key) => typeof meta[key] === 'number' ? { [key]: meta[key] } : {};
|
|
906
|
+
return {
|
|
907
|
+
src: sources[0].src,
|
|
908
|
+
format: sources[0].format,
|
|
909
|
+
sources,
|
|
910
|
+
...numeric('width'),
|
|
911
|
+
...numeric('height'),
|
|
912
|
+
...numeric('durationSeconds'),
|
|
913
|
+
};
|
|
914
|
+
}
|
|
915
|
+
/**
|
|
916
|
+
* URL of one file of a streaming package on the stream route — signed for the whole package
|
|
917
|
+
* with `delivery.signing`. {@link stream} builds the entry URLs with it.
|
|
918
|
+
*/
|
|
919
|
+
streamUrl(record, conversion, file, options = {}) {
|
|
920
|
+
const path = this.deliveryStreamPath
|
|
921
|
+
.replace(':id', encodeURIComponent(record.id))
|
|
922
|
+
.replace(':conversion', encodeURIComponent(conversion));
|
|
923
|
+
const url = `${path.replace(/\/+$/, '')}/${file.split('/').map(encodeURIComponent).join('/')}`;
|
|
924
|
+
const query = this.streamQuery(record, conversion, options.expiresIn);
|
|
925
|
+
return query ? `${url}?${query}` : url;
|
|
926
|
+
}
|
|
927
|
+
/**
|
|
928
|
+
* The `exp=…&sig=…` that authorizes every file of one streaming package (no `?`), or `''`
|
|
929
|
+
* without `delivery.signing`. The signature covers the record and the conversion — not a file —
|
|
930
|
+
* so it can ride along on every reference a manifest makes.
|
|
931
|
+
*/
|
|
932
|
+
streamQuery(record, conversion, expiresIn) {
|
|
933
|
+
if (!this.deliverySigning)
|
|
934
|
+
return '';
|
|
935
|
+
const duration = record.conversions[conversion]?.meta?.durationSeconds;
|
|
936
|
+
const ttl = Math.max(expiresIn ?? this.deliverySigning.expiresIn ?? DEFAULT_DELIVERY_SIGNING_TTL_SECONDS, typeof duration === 'number' ? Math.ceil(duration) + 3600 : 0);
|
|
937
|
+
const expires = deliveryExpiry(this.now().getTime(), ttl);
|
|
938
|
+
const signature = signDelivery(this.deliverySigning.secret, {
|
|
939
|
+
mediaId: record.id,
|
|
940
|
+
conversion,
|
|
941
|
+
expires,
|
|
942
|
+
});
|
|
943
|
+
return deliveryQueryString({ expires, signature }).slice(1);
|
|
944
|
+
}
|
|
945
|
+
/**
|
|
946
|
+
* Serve one file of a record's streaming package — the "how" of `media.access.streamRoute()`
|
|
947
|
+
* (authorize first). `file` is checked against the package's persisted artifact list, so a
|
|
948
|
+
* request can never reach outside it.
|
|
949
|
+
*
|
|
950
|
+
* - A **manifest** (`.m3u8`/`.mpd`) comes back as text. Its references are relative and resolve
|
|
951
|
+
* to the same route, so it is served as stored — plus `query` (a package signature) on every
|
|
952
|
+
* relative reference when given, which is how segment requests inherit the authorization.
|
|
953
|
+
* - Anything else (segments, init sections) is delivered per `mode` (default `delivery.mode`):
|
|
954
|
+
* a redirect to the object (signed URLs stay short-lived — they are minted per segment
|
|
955
|
+
* request), or a stream through the app.
|
|
956
|
+
*/
|
|
957
|
+
async streamFile(record, conversion, file, options = {}) {
|
|
958
|
+
const variant = record.conversions[conversion];
|
|
959
|
+
if (!variant)
|
|
960
|
+
throw new TransformNotReadyError(record.id, conversion);
|
|
961
|
+
if (variant.path === undefined || variant.disk === undefined || !variant.files) {
|
|
962
|
+
throw new ConversionArtifactMissingError(record.id, conversion);
|
|
963
|
+
}
|
|
964
|
+
// Membership in the persisted artifact list is the whole path check: caller input never
|
|
965
|
+
// reaches a disk key by arithmetic, so traversal is structurally impossible.
|
|
966
|
+
if (!variant.files.includes(file)) {
|
|
967
|
+
throw new MediaNotFoundError(`${record.id}#${conversion}:${file}`);
|
|
968
|
+
}
|
|
969
|
+
const prefix = variant.prefix ?? variant.path.slice(0, variant.path.lastIndexOf('/') + 1);
|
|
970
|
+
const disk = this.storage.disk(variant.disk);
|
|
971
|
+
const key = `${prefix}${file}`;
|
|
972
|
+
const fileName = file.slice(file.lastIndexOf('/') + 1);
|
|
973
|
+
const format = streamFormatOf(file);
|
|
974
|
+
if (format) {
|
|
975
|
+
const stored = Buffer.from(await disk.getBytes(key)).toString('utf8');
|
|
976
|
+
return {
|
|
977
|
+
kind: 'manifest',
|
|
978
|
+
content: await signManifest(file, stored, options.query ?? ''),
|
|
979
|
+
contentType: format === 'hls' ? HLS_MIME_TYPE : DASH_MIME_TYPE,
|
|
980
|
+
fileName,
|
|
981
|
+
};
|
|
982
|
+
}
|
|
983
|
+
const mode = await resolveDeliveryMode(options.mode ?? this.deliveryMode, disk, key);
|
|
984
|
+
if (mode === 'public')
|
|
985
|
+
return { kind: 'redirect', url: await disk.getUrl(key) };
|
|
986
|
+
if (mode === 'signed') {
|
|
987
|
+
const expiresIn = options.signedTtlSeconds ?? this.deliverySignedTtlSeconds;
|
|
988
|
+
return { kind: 'redirect', url: await disk.getSignedUrl(key, { expiresIn }) };
|
|
989
|
+
}
|
|
990
|
+
const metadata = await disk.getMetaData(key);
|
|
991
|
+
return {
|
|
992
|
+
kind: 'stream',
|
|
993
|
+
stream: await disk.getStream(key),
|
|
994
|
+
// A disk that stores everything as a generic type (S3 multipart) would make a player
|
|
995
|
+
// refuse the segment; the extension knows better.
|
|
996
|
+
mimeType: metadata.contentType && metadata.contentType !== 'application/octet-stream'
|
|
997
|
+
? metadata.contentType
|
|
998
|
+
: (streamArtifactContentType(file) ?? 'application/octet-stream'),
|
|
999
|
+
size: metadata.contentLength,
|
|
1000
|
+
fileName,
|
|
1001
|
+
};
|
|
1002
|
+
}
|
|
871
1003
|
/**
|
|
872
1004
|
* Check a delivery request against `delivery.signing`: `true` when it is signed and valid (serve
|
|
873
1005
|
* any size it names), `false` when signing is off or the request is unsigned and that is allowed.
|
|
@@ -930,7 +1062,7 @@ export class MediaLibrary {
|
|
|
930
1062
|
return {
|
|
931
1063
|
kind: 'stream',
|
|
932
1064
|
stream: await disk.getStream(target.path),
|
|
933
|
-
mimeType: metadata.contentType
|
|
1065
|
+
mimeType: deliveredMimeType(metadata.contentType, target.record.mimeType, conversion),
|
|
934
1066
|
size: metadata.contentLength,
|
|
935
1067
|
fileName: target.fileName,
|
|
936
1068
|
...vary,
|
|
@@ -1006,4 +1138,18 @@ function focalPointProp(record) {
|
|
|
1006
1138
|
const focalPoint = focalPointOf(record.customProperties);
|
|
1007
1139
|
return focalPoint ? { focalPoint } : {};
|
|
1008
1140
|
}
|
|
1141
|
+
const GENERIC_CONTENT_TYPES = new Set(['', 'application/octet-stream', 'binary/octet-stream']);
|
|
1142
|
+
/**
|
|
1143
|
+
* The `Content-Type` of a proxied file. The disk's own type wins — for a conversion the record
|
|
1144
|
+
* describes the ORIGINAL (a `thumb` of a PNG may be WebP) — unless it is a generic one: an object
|
|
1145
|
+
* assembled by a multipart/TUS upload comes back from S3 as `application/octet-stream`, and a PDF
|
|
1146
|
+
* served that way opens in no viewer. For the original, the record's type (checked against the
|
|
1147
|
+
* file's signature on attach) is then the truth.
|
|
1148
|
+
*/
|
|
1149
|
+
function deliveredMimeType(fromDisk, fromRecord, conversion) {
|
|
1150
|
+
const base = (fromDisk ?? '').split(';')[0].trim().toLowerCase();
|
|
1151
|
+
if (!GENERIC_CONTENT_TYPES.has(base))
|
|
1152
|
+
return fromDisk;
|
|
1153
|
+
return conversion === undefined ? fromRecord : fromDisk || 'application/octet-stream';
|
|
1154
|
+
}
|
|
1009
1155
|
//# sourceMappingURL=media_library.js.map
|