Files
drive/packages/convex/files.ts

112 lines
2.6 KiB
TypeScript
Raw Normal View History

import type { Id } from "@fileone/convex/dataModel"
2025-10-20 00:17:50 +00:00
import { v } from "convex/values"
import {
authenticatedMutation,
authenticatedQuery,
authorizedGet,
} from "./functions"
2025-09-13 22:02:27 +01:00
import * as Directories from "./model/directories"
2025-09-18 00:14:16 +00:00
import * as Files from "./model/files"
2025-09-13 22:02:27 +01:00
2025-09-15 21:44:41 +00:00
export const generateUploadUrl = authenticatedMutation({
2025-09-13 22:02:27 +01:00
handler: async (ctx) => {
return await ctx.storage.generateUploadUrl()
},
})
2025-09-15 21:44:41 +00:00
export const fetchFiles = authenticatedQuery({
2025-09-13 22:02:27 +01:00
args: {
directoryId: v.optional(v.id("directories")),
},
handler: async (ctx, { directoryId }) => {
return await ctx.db
.query("files")
2025-09-16 22:36:26 +00:00
.withIndex("byDirectoryId", (q) =>
q.eq("userId", ctx.user._id).eq("directoryId", directoryId),
)
2025-09-13 22:02:27 +01:00
.collect()
},
})
export const fetchRootDirectory = authenticatedQuery({
handler: async (ctx) => {
return await Directories.fetchRoot(ctx)
},
})
export const fetchDirectory = authenticatedQuery({
args: {
directoryId: v.id("directories"),
},
handler: async (ctx, { directoryId }) => {
const directory = await authorizedGet(ctx, directoryId)
if (!directory) {
throw new Error("Directory not found")
}
return await Directories.fetch(ctx, { directoryId })
},
})
2025-09-15 21:44:41 +00:00
export const createDirectory = authenticatedMutation({
2025-09-13 22:02:27 +01:00
args: {
name: v.string(),
directoryId: v.id("directories"),
2025-09-13 22:02:27 +01:00
},
handler: async (ctx, { name, directoryId }): Promise<Id<"directories">> => {
const parentDirectory = await authorizedGet(ctx, directoryId)
if (!parentDirectory) {
throw new Error("Parent directory not found")
}
2025-10-20 00:17:50 +00:00
2025-09-15 21:44:41 +00:00
return await Directories.create(ctx, {
name,
parentId: directoryId,
})
2025-09-13 22:02:27 +01:00
},
})
2025-09-15 21:44:41 +00:00
export const saveFile = authenticatedMutation({
2025-09-13 22:02:27 +01:00
args: {
name: v.string(),
size: v.number(),
2025-09-19 23:17:13 +00:00
directoryId: v.id("directories"),
2025-09-13 22:02:27 +01:00
storageId: v.id("_storage"),
2025-09-14 18:49:28 +00:00
mimeType: v.optional(v.string()),
2025-09-13 22:02:27 +01:00
},
2025-09-15 21:44:41 +00:00
handler: async (ctx, { name, storageId, directoryId, size, mimeType }) => {
const directory = await authorizedGet(ctx, directoryId)
if (!directory) {
throw new Error("Directory not found")
}
2025-10-20 00:17:50 +00:00
const now = Date.now()
2025-09-15 21:44:41 +00:00
2025-09-13 22:02:27 +01:00
await ctx.db.insert("files", {
name,
size,
storageId,
directoryId,
2025-09-15 21:44:41 +00:00
userId: ctx.user._id,
2025-09-14 18:49:28 +00:00
mimeType,
2025-09-13 22:02:27 +01:00
createdAt: now,
updatedAt: now,
})
},
})
2025-09-18 00:14:16 +00:00
export const renameFile = authenticatedMutation({
args: {
directoryId: v.optional(v.id("directories")),
itemId: v.id("files"),
newName: v.string(),
},
handler: async (ctx, { directoryId, itemId, newName }) => {
const file = await authorizedGet(ctx, itemId)
if (!file) {
throw new Error("File not found")
}
2025-10-20 00:17:50 +00:00
2025-09-18 00:14:16 +00:00
await Files.renameFile(ctx, { directoryId, itemId, newName })
},
})