2025-09-13 22:02:27 +01:00
|
|
|
import { v } from "convex/values"
|
2025-09-28 15:45:49 +00:00
|
|
|
import type { Id } from "./_generated/dataModel"
|
2025-09-15 21:44:41 +00:00
|
|
|
import { authenticatedMutation, authenticatedQuery } 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-28 15:45:49 +00:00
|
|
|
import type { FileSystemItem } from "./model/filesystem"
|
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) => {
|
2025-09-15 21:44:41 +00:00
|
|
|
// ctx.user and ctx.identity are automatically available
|
2025-09-13 22:02:27 +01:00
|
|
|
return await ctx.storage.generateUploadUrl()
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
2025-09-20 19:55:20 +00:00
|
|
|
export const generateFileUrl = authenticatedQuery({
|
|
|
|
|
args: {
|
|
|
|
|
storageId: v.id("_storage"),
|
|
|
|
|
},
|
|
|
|
|
handler: async (ctx, { storageId }) => {
|
|
|
|
|
return await ctx.storage.getUrl(storageId)
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
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()
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
2025-09-19 23:01:44 +00:00
|
|
|
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 }) => {
|
|
|
|
|
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(),
|
2025-09-19 23:01:44 +00:00
|
|
|
directoryId: v.id("directories"),
|
2025-09-13 22:02:27 +01:00
|
|
|
},
|
|
|
|
|
handler: async (ctx, { name, directoryId }): Promise<Id<"directories">> => {
|
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 }) => {
|
2025-10-03 21:23:51 +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-14 10:59:49 +00:00
|
|
|
|
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 }) => {
|
|
|
|
|
await Files.renameFile(ctx, { directoryId, itemId, newName })
|
|
|
|
|
},
|
|
|
|
|
})
|