-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(large-file-upload): auth and file record
- Loading branch information
Showing
26 changed files
with
699 additions
and
675 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
apps/large-file-upload/prisma/migrations/20240812105857_update_file_state/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
-- AlterTable | ||
ALTER TABLE "File" ALTER COLUMN "state" SET DEFAULT '', | ||
ALTER COLUMN "state" SET DATA TYPE TEXT; |
14 changes: 14 additions & 0 deletions
14
apps/large-file-upload/prisma/migrations/20240812111517_/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* | ||
Warnings: | ||
- You are about to drop the column `chunkSize` on the `File` table. All the data in the column will be lost. | ||
- You are about to drop the column `concurrency` on the `File` table. All the data in the column will be lost. | ||
- You are about to drop the column `poolElapse` on the `File` table. All the data in the column will be lost. | ||
- You are about to drop the column `state` on the `File` table. All the data in the column will be lost. | ||
*/ | ||
-- AlterTable | ||
ALTER TABLE "File" DROP COLUMN "chunkSize", | ||
DROP COLUMN "concurrency", | ||
DROP COLUMN "poolElapse", | ||
DROP COLUMN "state"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
"use server"; | ||
|
||
import { log } from "@/log"; | ||
import { prisma } from "@/prisma/client"; | ||
import { IUploadClientJSON } from "@npcs/upload"; | ||
import { getCurrentUser, getCurrentUserOrThrow } from "./user"; | ||
|
||
export async function addFile(info: IUploadClientJSON) { | ||
log.log(info); | ||
|
||
const user = await getCurrentUserOrThrow(); | ||
const existingFile = await prisma.file.findFirst({ | ||
where: { | ||
name: info.name, | ||
hash: info.hash, | ||
}, | ||
}); | ||
|
||
if (existingFile) { | ||
await prisma.user.update({ | ||
where: user, | ||
data: { | ||
files: { | ||
update: { | ||
where: { | ||
id: existingFile.id, | ||
}, | ||
data: info, | ||
}, | ||
}, | ||
}, | ||
}); | ||
} else { | ||
await prisma.file.create({ | ||
data: { | ||
...info, | ||
users: { | ||
connect: { | ||
id: user.id, | ||
}, | ||
}, | ||
}, | ||
}); | ||
} | ||
} | ||
|
||
export async function getFiles() { | ||
const user = await getCurrentUser(); | ||
if (!user) { | ||
return []; | ||
} | ||
|
||
const userWithFiles = await prisma.user.findFirst({ | ||
where: { | ||
id: user.id, | ||
}, | ||
include: { | ||
files: true, | ||
}, | ||
}); | ||
|
||
return userWithFiles?.files ?? []; | ||
} | ||
|
||
export async function removeFile(info: IUploadClientJSON) { | ||
const user = await getCurrentUserOrThrow(); | ||
|
||
const existingFile = await prisma.file.findFirstOrThrow({ | ||
where: { | ||
name: info.name, | ||
hash: info.hash, | ||
}, | ||
}); | ||
|
||
await prisma.user.update({ | ||
where: { | ||
id: user.id, | ||
}, | ||
data: { | ||
files: { | ||
disconnect: existingFile, | ||
}, | ||
}, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import { createLog } from "@npcs/log"; | ||
import { env } from "@npcs/env/shared"; | ||
import { createLog, LogLevels } from "@npcs/log"; | ||
|
||
export const log = createLog("large-file-upload"); | ||
log.level = env.NODE_ENV === "development" ? LogLevels.debug : LogLevels.info; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.