Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 28 additions & 37 deletions convex/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const getFile = query({

const file = await ctx.db.get("files", args.id);

if (!file) {
if (!file) {
throw new Error("File not found");
}

Expand Down Expand Up @@ -84,7 +84,7 @@ export const getFilePath = query({
let currentId: Id<"files"> | undefined = args.id;

while (currentId) {
const file = (await ctx.db.get("files", currentId)) as
const file = (await ctx.db.get("files", currentId)) as
| Doc<"files">
| undefined;
if (!file) break;
Expand All @@ -98,7 +98,7 @@ export const getFilePath = query({
});

export const getFolderContents = query({
args: {
args: {
projectId: v.id("projects"),
parentId: v.optional(v.id("files")),
},
Expand All @@ -118,9 +118,7 @@ export const getFolderContents = query({
const files = await ctx.db
.query("files")
.withIndex("by_project_parent", (q) =>
q
.eq("projectId", args.projectId)
.eq("parentId", args.parentId)
q.eq("projectId", args.projectId).eq("parentId", args.parentId),
)
.collect();

Expand All @@ -137,7 +135,7 @@ export const getFolderContents = query({
});

export const createFile = mutation({
args: {
args: {
projectId: v.id("projects"),
parentId: v.optional(v.id("files")),
name: v.string(),
Expand All @@ -160,14 +158,12 @@ export const createFile = mutation({
const files = await ctx.db
.query("files")
.withIndex("by_project_parent", (q) =>
q
.eq("projectId", args.projectId)
.eq("parentId", args.parentId)
q.eq("projectId", args.projectId).eq("parentId", args.parentId),
)
.collect();

const existing = files.find(
(file) => file.name === args.name && file.type === "file"
(file) => file.name === args.name && file.type === "file",
);

if (existing) throw new Error("File already exists");
Expand All @@ -190,7 +186,7 @@ export const createFile = mutation({
});

export const createFolder = mutation({
args: {
args: {
projectId: v.id("projects"),
parentId: v.optional(v.id("files")),
name: v.string(),
Expand All @@ -212,14 +208,12 @@ export const createFolder = mutation({
const files = await ctx.db
.query("files")
.withIndex("by_project_parent", (q) =>
q
.eq("projectId", args.projectId)
.eq("parentId", args.parentId)
q.eq("projectId", args.projectId).eq("parentId", args.parentId),
)
.collect();

const existing = files.find(
(file) => file.name === args.name && file.type === "folder"
(file) => file.name === args.name && file.type === "folder",
);

if (existing) throw new Error("Folder already exists");
Expand Down Expand Up @@ -266,22 +260,21 @@ export const renameFile = mutation({
const siblings = await ctx.db
.query("files")
.withIndex("by_project_parent", (q) =>
q
.eq("projectId", file.projectId)
.eq("parentId", file.parentId)
q.eq("projectId", file.projectId).eq("parentId", file.parentId),
)
.collect();

const existing = siblings.find(
(sibling) =>
const existing = siblings.find((sibling) => {
return (
sibling.name === args.newName &&
sibling.type === file.type &&
sibling._id !== args.id
);
);
});

if (existing) {
throw new Error(
`A ${file.type} with this name already exists in this location`
`A ${file.type} with this name already exists in this location`,
);
}

Expand All @@ -296,7 +289,7 @@ export const renameFile = mutation({
await ctx.db.patch("projects", file.projectId, {
updatedAt: now,
});
}
},
});

export const deleteFile = mutation({
Expand Down Expand Up @@ -329,23 +322,21 @@ export const deleteFile = mutation({
}

// If it's a folder, delete all children first
if (item.type === "folder") {
const children = await ctx.db
if (item.type === "folder") {
const children = await ctx.db
.query("files")
.withIndex("by_project_parent", (q) =>
q
.eq("projectId", item.projectId)
.eq("parentId", fileId)
q.eq("projectId", item.projectId).eq("parentId", fileId),
)
.collect();

for (const child of children) {
await deleteRecursive(child._id);
}
}
for (const child of children) {
await deleteRecursive(child._id);
}
}

// Delete storage file if it exists
if (item.storageId) {
// Delete storage file if it exists
if (item.storageId) {
await ctx.storage.delete(item.storageId);
}

Expand All @@ -358,7 +349,7 @@ export const deleteFile = mutation({
await ctx.db.patch("projects", file.projectId, {
updatedAt: Date.now(),
});
}
},
});

export const updateFile = mutation({
Expand Down Expand Up @@ -394,4 +385,4 @@ export const updateFile = mutation({
updatedAt: now,
});
},
});
});