64 lines
2.3 KiB
JavaScript
64 lines
2.3 KiB
JavaScript
import { addFile } from "../../../../services/drive/add-file.js";
|
|
import { DriveFiles } from "../../../../models/index.js";
|
|
import { MastoApiError } from "../middleware/catch-errors.js";
|
|
import { toSingleLast } from "../../../../prelude/array.js";
|
|
export class MediaHelpers {
|
|
static async uploadMedia(ctx) {
|
|
const files = ctx.request.files;
|
|
const file = toSingleLast(files?.file);
|
|
const user = ctx.user;
|
|
const body = ctx.request.body;
|
|
if (!file) throw new MastoApiError(400, "Validation failed: File content type is invalid, File is invalid");
|
|
return addFile({
|
|
user: user,
|
|
path: file.filepath,
|
|
name: file.originalFilename !== null && file.originalFilename !== 'file' ? file.originalFilename : undefined,
|
|
comment: body?.description ?? undefined,
|
|
sensitive: false
|
|
}).then((p)=>DriveFiles.pack(p));
|
|
}
|
|
static async uploadMediaBasic(file, ctx) {
|
|
const user = ctx.user;
|
|
return addFile({
|
|
user: user,
|
|
path: file.filepath,
|
|
name: file.originalFilename !== null && file.originalFilename !== 'file' ? file.originalFilename : undefined,
|
|
sensitive: false
|
|
});
|
|
}
|
|
static async updateMedia(file, ctx) {
|
|
const user = ctx.user;
|
|
const body = ctx.request.body;
|
|
await DriveFiles.update(file.id, {
|
|
comment: body?.description ?? undefined
|
|
});
|
|
return DriveFiles.findOneByOrFail({
|
|
id: file.id,
|
|
userId: user.id
|
|
}).then((p)=>DriveFiles.pack(p));
|
|
}
|
|
static async getMediaPacked(id, ctx) {
|
|
const user = ctx.user;
|
|
return this.getMedia(id, ctx).then((p)=>p ? DriveFiles.pack(p) : null);
|
|
}
|
|
static async getMediaPackedOr404(id, ctx) {
|
|
return this.getMediaPacked(id, ctx).then((p)=>{
|
|
if (p) return p;
|
|
throw new MastoApiError(404);
|
|
});
|
|
}
|
|
static async getMedia(id, ctx) {
|
|
const user = ctx.user;
|
|
return DriveFiles.findOneBy({
|
|
id: id,
|
|
userId: user.id
|
|
});
|
|
}
|
|
static async getMediaOr404(id, ctx) {
|
|
return this.getMedia(id, ctx).then((p)=>{
|
|
if (p) return p;
|
|
throw new MastoApiError(404);
|
|
});
|
|
}
|
|
}
|