38 lines
1.8 KiB
TypeScript
38 lines
1.8 KiB
TypeScript
export default defineEventHandler(async (event) => {
|
|
const { owner } = assertLibraryOwner(event)
|
|
const id = String(getRouterParam(event, 'id') || '')
|
|
const body = await readBody<{ folderId?: string; name?: string; family?: boolean; globalLocks?: string; permanenceRefs?: unknown; hideThumbnail?: boolean }>(event).catch(() => ({} as { folderId?: string; name?: string; family?: boolean; globalLocks?: string; permanenceRefs?: unknown; hideThumbnail?: boolean }))
|
|
const folderId = String(body?.folderId || '')
|
|
const hasName = typeof body?.name === 'string'
|
|
const family = body?.family === true
|
|
const hasLocks = typeof body?.globalLocks === 'string'
|
|
const hasRefs = Array.isArray(body?.permanenceRefs)
|
|
const hasHide = typeof body?.hideThumbnail === 'boolean'
|
|
if (!folderId && !hasName && !hasLocks && !hasRefs && !hasHide) {
|
|
throw createError({ statusCode: 400, statusMessage: 'Choose a library folder or a new name' })
|
|
}
|
|
const clip = getClip(owner, id)
|
|
assertFolderAccess(event, clip.folderId)
|
|
if (folderId) {
|
|
assertFolderExists(event, folderId)
|
|
if (family) await moveClipFamily(owner, id, folderId)
|
|
else await moveClip(owner, id, folderId)
|
|
}
|
|
if (hasName) {
|
|
if (family) await renameClipFamily(owner, id, body.name || '')
|
|
else await renameClip(owner, id, body.name || '')
|
|
}
|
|
if (hasLocks || hasRefs) {
|
|
await patchClipMeta(owner, id, {
|
|
globalLocks: hasLocks ? body.globalLocks : undefined,
|
|
permanenceRefs: hasRefs ? body.permanenceRefs as import('~/utils/globalLocks').PermanenceRef[] : undefined,
|
|
family: family || hasLocks
|
|
})
|
|
}
|
|
if (hasHide) await setClipHidden(owner, id, body.hideThumbnail === true, family)
|
|
if (hasHide && !folderId && !hasName && !hasLocks && !hasRefs) {
|
|
return { id, hideThumbnail: body.hideThumbnail === true }
|
|
}
|
|
return publicLibrary(event)
|
|
})
|