t3xbu10dr: Publish video posts through the media pipeline and upgrade thumbnails to the player
This commit is contained in:
+23
-1
@@ -76,7 +76,8 @@ function toMedia(raw: unknown): CardMedia[] {
|
||||
if (m.mediaType === "video") {
|
||||
const thumb = str(m.thumbnailUrl);
|
||||
const hls = muxHlsUrl(m.hlsUrl);
|
||||
if (thumb || hls) out.push({ kind: "video", src: thumb, hlsUrl: hls });
|
||||
// Keep a video even with no thumbnail or hlsUrl yet (just published).
|
||||
out.push({ kind: "video", src: thumb, hlsUrl: hls });
|
||||
continue;
|
||||
}
|
||||
const src = str(m.imageMediumWebpUrl) ?? str(m.imageMediumUrl) ?? str(m.imageUrl);
|
||||
@@ -142,6 +143,27 @@ export function mergePosts(...lists: CardPost[][]): CardPost[] {
|
||||
return sortByPublishedDesc(all);
|
||||
}
|
||||
|
||||
const hasHls = (p: CardPost) => p.media.some((m) => m.kind === "video" && m.hlsUrl);
|
||||
|
||||
/**
|
||||
* Swap in the media of shown posts whose video gained an hlsUrl on a refresh
|
||||
* (transcoding finished), so the thumbnail upgrades to the player in place.
|
||||
* Returns the same array when nothing changed.
|
||||
*/
|
||||
export function upgradeVideos(current: CardPost[], fresh: CardPost[]): CardPost[] {
|
||||
const byKey = new Map(fresh.map((p) => [p.key, p]));
|
||||
let changed = false;
|
||||
const next = current.map((p) => {
|
||||
const f = byKey.get(p.key);
|
||||
if (f && !hasHls(p) && hasHls(f)) {
|
||||
changed = true;
|
||||
return { ...p, media: f.media };
|
||||
}
|
||||
return p;
|
||||
});
|
||||
return changed ? next : current;
|
||||
}
|
||||
|
||||
export function relativeTime(publishedAt: string | null, now: number = Date.now()): string {
|
||||
const t = publishedAt ? Date.parse(publishedAt) : NaN;
|
||||
if (!Number.isFinite(t)) return "";
|
||||
|
||||
+46
-2
@@ -9,8 +9,8 @@ import type { SessionData } from "./session-store";
|
||||
// 3. the SERVER PUTs the bytes to that URL (the browser never gets the token)
|
||||
// 4. confirm the upload (image confirmation blocks until processing finishes)
|
||||
// 5. publish with the same {content} publish call as a text post
|
||||
// The pipeline is media-type agnostic so the video task can reuse it; only the
|
||||
// allowed-type gate and the public entry point here are image-specific.
|
||||
// The pipeline is media-type agnostic; the allowed-type gates and entry points
|
||||
// (createImagePost, createVideoPost) are per kind.
|
||||
// Neither the feed token nor the file bytes are ever logged.
|
||||
|
||||
export type MediaKind = "image" | "video";
|
||||
@@ -25,6 +25,12 @@ export const ALLOWED_IMAGE_TYPES: readonly string[] = [
|
||||
"image/heic",
|
||||
];
|
||||
|
||||
/** Video content types the feed accepts. */
|
||||
export const ALLOWED_VIDEO_TYPES: readonly string[] = ["video/mp4", "video/quicktime", "video/webm"];
|
||||
|
||||
/** Upper bound on an uploaded video, enforced before any feed call. */
|
||||
export const MAX_VIDEO_BYTES = 200 * 1024 * 1024;
|
||||
|
||||
/** Upper bound on an uploaded image, enforced before any feed call. */
|
||||
export const MAX_IMAGE_BYTES = 15 * 1024 * 1024;
|
||||
|
||||
@@ -112,6 +118,44 @@ export function isAllowedImageType(type: string): boolean {
|
||||
return ALLOWED_IMAGE_TYPES.includes(type.trim().toLowerCase());
|
||||
}
|
||||
|
||||
/** True when the type is an accepted video content type (case-insensitive). */
|
||||
export function isAllowedVideoType(type: string): boolean {
|
||||
return ALLOWED_VIDEO_TYPES.includes(type.trim().toLowerCase());
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a public video post: same pipeline as an image with type "video".
|
||||
* Video confirmation may return while transcoding continues; the post is
|
||||
* published anyway and the card shows the thumbnail until hlsUrl appears.
|
||||
*/
|
||||
export async function createVideoPost(
|
||||
sessionId: string | undefined,
|
||||
content: unknown,
|
||||
file: MediaUpload["file"] | null,
|
||||
options: RefreshOptions = {},
|
||||
): Promise<CreateMediaPostResult> {
|
||||
if (!file || typeof file.size !== "number" || file.size <= 0 || file.size > MAX_VIDEO_BYTES) {
|
||||
return { ok: false, status: 400, error: "bad_file" };
|
||||
}
|
||||
if (typeof file.type !== "string" || !isAllowedVideoType(file.type)) {
|
||||
return { ok: false, status: 400, error: "bad_file" };
|
||||
}
|
||||
return runMediaPipeline(sessionId, content, { file, mediaType: "video" }, options);
|
||||
}
|
||||
|
||||
/** Route entry point: picks image or video from the file type; anything else is a 400. */
|
||||
export async function createMediaPost(
|
||||
sessionId: string | undefined,
|
||||
content: unknown,
|
||||
file: MediaUpload["file"] | null,
|
||||
options: RefreshOptions = {},
|
||||
): Promise<CreateMediaPostResult> {
|
||||
if (file && typeof file.type === "string" && isAllowedVideoType(file.type)) {
|
||||
return createVideoPost(sessionId, content, file, options);
|
||||
}
|
||||
return createImagePost(sessionId, content, file, options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a public image post from a caption and one file: draft -> presigned URL
|
||||
* -> server-side PUT -> confirm -> publish. Returns the post id so the caller can
|
||||
|
||||
Reference in New Issue
Block a user