fix: use /api/v2/media (#2121)

* fix: use /api/v2/media

Fixes #2078

* fix: fix comment
This commit is contained in:
Nolan Lawson 2022-04-10 10:35:24 -07:00 committed by GitHub
parent 10ed291950
commit a9119fa53f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,17 +1,41 @@
import { auth, basename } from './utils.js'
import { post, put, MEDIA_WRITE_TIMEOUT, WRITE_TIMEOUT } from '../_utils/ajax.js'
export async function uploadMedia (instanceName, accessToken, file, description) {
async function doUploadMedia (version, instanceName, accessToken, file, description) {
const formData = new FormData()
formData.append('file', file)
if (description) {
formData.append('description', description)
}
const url = `${basename(instanceName)}/api/v1/media`
const url = `${basename(instanceName)}/api/${version}/media`
return post(url, formData, auth(accessToken), { timeout: MEDIA_WRITE_TIMEOUT })
}
export async function putMediaMetadata (instanceName, accessToken, mediaId, description, focus) {
const url = `${basename(instanceName)}/api/v1/media/${mediaId}`
async function doPutMediaMetadata (version, instanceName, accessToken, mediaId, description, focus) {
const url = `${basename(instanceName)}/api/${version}/media/${mediaId}`
return put(url, { description, focus: (focus && focus.join(',')) }, auth(accessToken), { timeout: WRITE_TIMEOUT })
}
export async function uploadMedia (instanceName, accessToken, file, description) {
try {
return (await doUploadMedia('v2', instanceName, accessToken, file, description))
} catch (err) {
if (err && err.status === 404) { // fall back to old v1 API
return doUploadMedia('v1', instanceName, accessToken, file, description)
} else {
throw err
}
}
}
export async function putMediaMetadata (instanceName, accessToken, mediaId, description, focus) {
try {
return (await doPutMediaMetadata('v2', instanceName, accessToken, mediaId, description, focus))
} catch (err) {
if (err && err.status === 404) { // fall back to old v1 API
return doPutMediaMetadata('v1', instanceName, accessToken, mediaId, description, focus)
} else {
throw err
}
}
}