6bf8bebf51
CI / Test and Build (push) Failing after 1s
CI / Migrate Dev DB (push) Has been skipped
CI / Migrate DB (push) Has been skipped
CodeQL / Analyze actions (push) Has been cancelled
CodeQL / Analyze javascript-typescript (push) Has been cancelled
CI / Detect Version (push) Has been cancelled
CI / Detect Desktop Changes (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/cron.Dockerfile, ubuntu-latest, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build AMD64 (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/cron.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/db.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/pii.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/realtime.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-8vcpu-ubuntu-2404-arm, ./docker/app.Dockerfile, linux-arm64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
Helm Chart / Lint, test, and validate chart (push) Has been cancelled
Helm Chart / Chart version bumped (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
CI / Build Dev ECR (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core) (push) Has been cancelled
CI / Promote Images (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Check Desktop Signing Secrets (push) Has been cancelled
CI / Desktop Release (push) Has been cancelled
CI / Create Desktop Prerelease (push) Has been cancelled
CI / Desktop Prerelease Build (push) Has been cancelled
CI / Publish Desktop Prerelease (push) Has been cancelled
CI / Prune Desktop Prereleases (push) Has been cancelled
Helm Chart / Install on kind and run helm test (push) Has been cancelled
295 lines
9.0 KiB
TypeScript
295 lines
9.0 KiB
TypeScript
import type { Logger } from '@sim/logger'
|
|
import { getErrorMessage } from '@sim/utils/errors'
|
|
import { NextResponse } from 'next/server'
|
|
import {
|
|
secureFetchWithPinnedIP,
|
|
validateUrlWithDNS,
|
|
} from '@/lib/core/security/input-validation.server'
|
|
import type { RawFileInput } from '@/lib/uploads/utils/file-schemas'
|
|
import { resolveFileInputToUrl } from '@/lib/uploads/utils/file-utils.server'
|
|
import {
|
|
BUFFER_API_URL,
|
|
BUFFER_POST_SELECTION,
|
|
bufferHeaders,
|
|
mapBufferPost,
|
|
parseBufferGraphQLResponse,
|
|
} from '@/tools/buffer/types'
|
|
|
|
const VIDEO_EXTENSIONS = ['.mp4', '.mov', '.m4v', '.webm', '.avi']
|
|
const IMAGE_EXTENSIONS = ['.png', '.jpg', '.jpeg', '.gif', '.webp']
|
|
const MEDIA_PROBE_TIMEOUT_MS = 5000
|
|
/**
|
|
* Buffer fetches asset URLs at publish time, which for queued or scheduled
|
|
* posts can be days after createPost. Presign stored files for the S3 maximum
|
|
* of 7 days so scheduled posts within that window can still be published.
|
|
* The effective lifetime is additionally bounded by the signing credentials:
|
|
* Sim's storage clients sign with static keys (AWS_ACCESS_KEY_ID /
|
|
* AWS_SECRET_ACCESS_KEY), which support the full 7 days; deployments signing
|
|
* with temporary session credentials cap every presigned URL at the session
|
|
* lifetime platform-wide.
|
|
*/
|
|
const MEDIA_PRESIGN_EXPIRY_SECONDS = 7 * 24 * 60 * 60
|
|
|
|
const CREATE_POST_MUTATION = `
|
|
mutation CreatePost($input: CreatePostInput!) {
|
|
createPost(input: $input) {
|
|
__typename
|
|
... on PostActionSuccess {
|
|
post {
|
|
${BUFFER_POST_SELECTION}
|
|
}
|
|
}
|
|
... on MutationError {
|
|
message
|
|
}
|
|
}
|
|
}
|
|
`
|
|
|
|
const EDIT_POST_MUTATION = `
|
|
mutation EditPost($input: EditPostInput!) {
|
|
editPost(input: $input) {
|
|
__typename
|
|
... on PostActionSuccess {
|
|
post {
|
|
${BUFFER_POST_SELECTION}
|
|
}
|
|
}
|
|
... on MutationError {
|
|
message
|
|
}
|
|
}
|
|
}
|
|
`
|
|
|
|
interface ResolveMediaAssetOptions {
|
|
media: RawFileInput | string
|
|
mediaType?: 'auto' | 'image' | 'video' | null
|
|
mediaAltText?: string | null
|
|
userId: string
|
|
requestId: string
|
|
logger: Logger
|
|
}
|
|
|
|
interface ResolvedMediaAsset {
|
|
asset?: Record<string, unknown>
|
|
errorResponse?: NextResponse
|
|
}
|
|
|
|
/**
|
|
* Classifies media by extension: 'video', 'image', or null when the
|
|
* path/URL has no recognizable media extension.
|
|
*/
|
|
function mediaKindFromExtension(pathOrName: string): 'image' | 'video' | null {
|
|
const lowered = pathOrName.toLowerCase().split(/[?#]/)[0]
|
|
if (VIDEO_EXTENSIONS.some((extension) => lowered.endsWith(extension))) return 'video'
|
|
if (IMAGE_EXTENSIONS.some((extension) => lowered.endsWith(extension))) return 'image'
|
|
return null
|
|
}
|
|
|
|
/**
|
|
* Determines whether media should be attached as a video or image asset.
|
|
* Prefers the file's MIME type, then the path/URL extension, and for
|
|
* extensionless URLs falls back to a DNS-pinned HEAD probe of the resolved
|
|
* URL's Content-Type. Returns null when nothing is conclusive so the caller
|
|
* can ask for an explicit media type instead of guessing.
|
|
*/
|
|
async function resolveMediaKind(
|
|
mimeType: string | undefined,
|
|
pathOrName: string,
|
|
fileUrl: string,
|
|
requestId: string,
|
|
logger: Logger
|
|
): Promise<'image' | 'video' | null> {
|
|
if (mimeType?.startsWith('video/')) return 'video'
|
|
if (mimeType?.startsWith('image/')) return 'image'
|
|
|
|
const extensionKind = mediaKindFromExtension(pathOrName)
|
|
if (extensionKind) return extensionKind
|
|
|
|
try {
|
|
const validation = await validateUrlWithDNS(fileUrl, 'media')
|
|
if (validation.isValid && validation.resolvedIP) {
|
|
const probe = await secureFetchWithPinnedIP(fileUrl, validation.resolvedIP, {
|
|
method: 'HEAD',
|
|
timeout: MEDIA_PROBE_TIMEOUT_MS,
|
|
})
|
|
const contentType = probe.headers.get('content-type') || ''
|
|
if (contentType.startsWith('video/')) return 'video'
|
|
if (contentType.startsWith('image/')) return 'image'
|
|
}
|
|
} catch (error) {
|
|
logger.warn(`[${requestId}] Media content-type probe was inconclusive`, {
|
|
error: getErrorMessage(error, 'probe failed'),
|
|
})
|
|
}
|
|
return null
|
|
}
|
|
|
|
/**
|
|
* Resolves a media input (uploaded file, file reference, or external URL) to a
|
|
* Buffer AssetInput. Buffer downloads assets from publicly accessible URLs, so
|
|
* stored files are verified for access and resolved to short-lived presigned
|
|
* URLs.
|
|
*/
|
|
export async function resolveMediaAsset(
|
|
options: ResolveMediaAssetOptions
|
|
): Promise<ResolvedMediaAsset> {
|
|
const { media, mediaType, mediaAltText, userId, requestId, logger } = options
|
|
|
|
const isFileInput = typeof media === 'object'
|
|
const resolution = await resolveFileInputToUrl({
|
|
file: isFileInput ? media : undefined,
|
|
filePath: isFileInput ? undefined : media,
|
|
userId,
|
|
requestId,
|
|
logger,
|
|
presignExpirySeconds: MEDIA_PRESIGN_EXPIRY_SECONDS,
|
|
})
|
|
if (resolution.error || !resolution.fileUrl) {
|
|
return {
|
|
errorResponse: NextResponse.json(
|
|
{ success: false, error: resolution.error?.message || 'Failed to resolve media file' },
|
|
{ status: resolution.error?.status || 400 }
|
|
),
|
|
}
|
|
}
|
|
|
|
const mimeType = isFileInput ? media.type : undefined
|
|
const pathOrName = isFileInput ? media.name || '' : media
|
|
const kind =
|
|
mediaType === 'image' || mediaType === 'video'
|
|
? mediaType
|
|
: await resolveMediaKind(mimeType, pathOrName, resolution.fileUrl, requestId, logger)
|
|
if (!kind) {
|
|
return {
|
|
errorResponse: NextResponse.json(
|
|
{
|
|
success: false,
|
|
error:
|
|
'Could not determine whether the media is an image or a video. Set mediaType to "image" or "video".',
|
|
},
|
|
{ status: 400 }
|
|
),
|
|
}
|
|
}
|
|
if (kind === 'video') {
|
|
return { asset: { video: { url: resolution.fileUrl } } }
|
|
}
|
|
|
|
const image: Record<string, unknown> = { url: resolution.fileUrl }
|
|
if (mediaAltText?.trim()) {
|
|
image.metadata = { altText: mediaAltText.trim() }
|
|
}
|
|
return { asset: { image } }
|
|
}
|
|
|
|
interface ExecutePostMutationOptions {
|
|
apiKey: string
|
|
mutation: typeof CREATE_POST_MUTATION | typeof EDIT_POST_MUTATION
|
|
input: Record<string, unknown>
|
|
requestId: string
|
|
logger: Logger
|
|
}
|
|
|
|
/**
|
|
* Executes a createPost/editPost mutation against the Buffer GraphQL API and
|
|
* maps the PostActionPayload union onto the route's response envelope.
|
|
*/
|
|
async function executePostMutation(options: ExecutePostMutationOptions): Promise<NextResponse> {
|
|
const { apiKey, mutation, input, requestId, logger } = options
|
|
|
|
let result: Record<string, any>
|
|
try {
|
|
const response = await fetch(BUFFER_API_URL, {
|
|
method: 'POST',
|
|
headers: bufferHeaders(apiKey),
|
|
body: JSON.stringify({ query: mutation, variables: { input } }),
|
|
})
|
|
const data = await parseBufferGraphQLResponse(response)
|
|
result = data.createPost ?? data.editPost
|
|
} catch (error) {
|
|
const message = getErrorMessage(error, 'Buffer API request failed')
|
|
logger.error(`[${requestId}] Buffer post mutation failed`, { error: message })
|
|
return NextResponse.json({ success: false, error: message }, { status: 502 })
|
|
}
|
|
|
|
if (result?.__typename !== 'PostActionSuccess' || !result.post) {
|
|
const message = result?.message || 'Buffer rejected the post'
|
|
logger.warn(`[${requestId}] Buffer rejected post mutation`, {
|
|
typename: result?.__typename,
|
|
error: message,
|
|
})
|
|
return NextResponse.json({ success: false, error: message }, { status: 400 })
|
|
}
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
output: { post: mapBufferPost(result.post) },
|
|
})
|
|
}
|
|
|
|
interface ForwardPostMutationOptions {
|
|
apiKey: string
|
|
postId?: string
|
|
channelId?: string
|
|
text?: string | null
|
|
mode: string
|
|
schedulingType: string
|
|
dueAt?: string | null
|
|
saveToDraft?: boolean | null
|
|
media?: RawFileInput | string | null
|
|
mediaType?: 'auto' | 'image' | 'video' | null
|
|
mediaAltText?: string | null
|
|
userId: string
|
|
requestId: string
|
|
logger: Logger
|
|
}
|
|
|
|
/**
|
|
* Builds the CreatePostInput/EditPostInput from a validated route body
|
|
* (resolving media to a fetchable URL) and forwards the mutation to Buffer.
|
|
* Passing `postId` selects the editPost mutation; otherwise createPost runs.
|
|
*/
|
|
export async function forwardPostMutation(
|
|
options: ForwardPostMutationOptions
|
|
): Promise<NextResponse> {
|
|
const { apiKey, postId, channelId, media, mediaType, mediaAltText, userId, requestId, logger } =
|
|
options
|
|
|
|
const input: Record<string, unknown> = {
|
|
mode: options.mode,
|
|
schedulingType: options.schedulingType,
|
|
}
|
|
if (postId) {
|
|
input.id = postId
|
|
} else {
|
|
input.channelId = channelId
|
|
input.assets = []
|
|
}
|
|
if (options.text != null && options.text !== '') input.text = options.text
|
|
if (options.dueAt) input.dueAt = options.dueAt
|
|
if (options.saveToDraft != null) input.saveToDraft = options.saveToDraft
|
|
|
|
if (media) {
|
|
const { asset, errorResponse } = await resolveMediaAsset({
|
|
media,
|
|
mediaType,
|
|
mediaAltText,
|
|
userId,
|
|
requestId,
|
|
logger,
|
|
})
|
|
if (errorResponse) return errorResponse
|
|
input.assets = [asset]
|
|
}
|
|
|
|
return executePostMutation({
|
|
apiKey,
|
|
mutation: postId ? EDIT_POST_MUTATION : CREATE_POST_MUTATION,
|
|
input,
|
|
requestId,
|
|
logger,
|
|
})
|
|
}
|