also looking at this
feat: crossposting — media (LinkedIn/IG/Threads), OAuth token refresh, and X/Twitter
#30
28 files
+1810
-109
| @@ -1,3 +1,11 @@ | ||
| 1 | import { mkdir, unlink } from 'node:fs/promises' | |
| 2 | import { join } from 'node:path' | |
| 3 | import process from 'node:process' | |
| 4 | import { env } from '@stacksjs/env' | |
| 5 | ||
| 6 | /** Uploaded images for queued posts live here until publish. */ | |
| 7 | export const MEDIA_DIR = join(process.cwd(), 'storage/app/postline-media') | |
| 8 | ||
| 1 | 9 | /** |
| 2 | 10 | * Read an uploaded image regardless of shape: the router's multipart |
| 3 | 11 | * parser stores raw Web `File` objects on `request.files`, while the |
| @@ -19,3 +27,88 @@ export async function readUploadedImage(uploaded: unknown): Promise<{ bytes: Uin | ||
| 19 | 27 | |
| 20 | 28 | return null |
| 21 | 29 | } |
| 30 | ||
| 31 | /** | |
| 32 | * A stored media filename is safe to serve only when it's a bare basename with | |
| 33 | * an extension and no path separators or `..` — i.e. exactly the shape | |
| 34 | * QueueService writes (`<uuid>.<ext>` / `<uuid>-<hex>.<ext>`). This is the | |
| 35 | * traversal guard for the public media route; anything else is rejected. | |
| 36 | */ | |
| 37 | export function isSafeMediaFilename(name: string): boolean { | |
| 38 | return typeof name === 'string' && /^[\w-]+\.[a-z0-9]+$/i.test(name) | |
| 39 | } | |
| 40 | ||
| 41 | /** Map a media filename extension to a content type for inline serving. */ | |
| 42 | export function mediaContentType(name: string): string { | |
| 43 | const ext = name.split('.').pop()?.toLowerCase() || '' | |
| 44 | switch (ext) { | |
| 45 | case 'png': return 'image/png' | |
| 46 | case 'gif': return 'image/gif' | |
| 47 | case 'webp': return 'image/webp' | |
| 48 | case 'jpg': | |
| 49 | case 'jpeg': return 'image/jpeg' | |
| 50 | default: return 'application/octet-stream' | |
| 51 | } | |
| 52 | } | |
| 53 | ||
| 54 | /** | |
| 55 | * Pure URL builder: given a public base and a stored filename, produce the | |
| 56 | * media URL a remote platform fetches — or null when the base is empty or the | |
| 57 | * filename is unsafe. Split out from `publicMediaUrl` so the URL shape is | |
| 58 | * unit-testable without touching env. | |
| 59 | */ | |
| 60 | export function buildMediaUrl(base: string, filename: string): string | null { | |
| 61 | if (!isSafeMediaFilename(filename)) return null | |
| 62 | const raw = String(base || '').trim() | |
| 63 | if (!raw) return null | |
| 64 | ||
| 65 | const withScheme = /^https?:\/\//i.test(raw) ? raw : `https://${raw}` | |
| 66 | const normalized = withScheme.replace(/\/+$/, '') | |
| 67 | return `${normalized}/postline/media?file=${encodeURIComponent(filename)}` | |
| 68 | } | |
| 69 | ||
| 70 | /** | |
| 71 | * The public URL Instagram/Threads fetch (via the Meta Graph API) for a stored | |
| 72 | * media file. Meta pulls the image server-side, so it must be reachable over | |
| 73 | * the public internet — returns null when no public base is configured | |
| 74 | * (`STORAGE_PUBLIC_URL` or `APP_URL`), in which case callers fall back to | |
| 75 | * byte-upload providers only. | |
| 76 | */ | |
| 77 | export function publicMediaUrl(filename: string): string | null { | |
| 78 | return buildMediaUrl(String(env.STORAGE_PUBLIC_URL || env.APP_URL || ''), filename) | |
| 79 | } | |
| 80 | ||
| 81 | /** Whether a public base is configured — i.e. whether URL-only providers can be | |
| 82 | * handed a fetchable image URL at all. */ | |
| 83 | export function hasPublicMediaBase(): boolean { | |
| 84 | return Boolean(String(env.STORAGE_PUBLIC_URL || env.APP_URL || '').trim()) | |
| 85 | } | |
| 86 | ||
| 87 | /** | |
| 88 | * Persist uploaded bytes under a fresh name so a URL-only provider (Instagram/ | |
| 89 | * Threads) can fetch them during an immediate publish, returning the file's | |
| 90 | * public URL. Returns null when no public base is configured (nothing is | |
| 91 | * written — there'd be no fetchable URL). The caller MUST `removeTempMedia` the | |
| 92 | * returned filename once publishing finishes. | |
| 93 | */ | |
| 94 | export async function persistTempMedia(bytes: Uint8Array, mimeType?: string): Promise<{ filename: string, url: string } | null> { | |
| 95 | if (!hasPublicMediaBase()) return null | |
| 96 | ||
| 97 | const ext = (mimeType || 'image/jpeg').split('/')[1]?.replace(/[^a-z0-9]/gi, '') || 'jpg' | |
| 98 | const filename = `tmp-${crypto.randomUUID()}.${ext}` | |
| 99 | await mkdir(MEDIA_DIR, { recursive: true }) | |
| 100 | await Bun.write(join(MEDIA_DIR, filename), bytes) | |
| 101 | ||
| 102 | const url = publicMediaUrl(filename) | |
| 103 | if (!url) { | |
| 104 | await removeTempMedia(filename) | |
| 105 | return null | |
| 106 | } | |
| 107 | return { filename, url } | |
| 108 | } | |
| 109 | ||
| 110 | /** Delete a file previously written by `persistTempMedia`. Never throws. */ | |
| 111 | export async function removeTempMedia(filename: string): Promise<void> { | |
| 112 | if (!isSafeMediaFilename(filename)) return | |
| 113 | await unlink(join(MEDIA_DIR, filename)).catch(() => {}) | |
| 114 | } | |