-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1b02c75
commit 5a317ad
Showing
29 changed files
with
390 additions
and
115 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
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 |
---|---|---|
|
@@ -45,4 +45,4 @@ | |
"breakpoints": true | ||
} | ||
] | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { handleApiRequestWithUser, notFound } from "@/lib/api-commons"; | ||
import { FileType } from "@/lib/db/schemas/file"; | ||
import { ThumbnailType } from "@/lib/db/schemas/thumbnail"; | ||
import { env } from "@/lib/env"; | ||
import { getFileById, updateFile } from "@/lib/helpers/file"; | ||
import { getUserRole, roles } from "@/lib/helpers/role"; | ||
import { getThumbnailById, updateThumbnail } from "@/lib/helpers/thumbnail"; | ||
import Logger from "@/lib/logger"; | ||
import { getFileName } from "@/lib/utils/file"; | ||
import { getFilePath, getFileThumbnailPath } from "@/lib/utils/paths"; | ||
import { storage } from "@/storage/create-storage"; | ||
import { NextRequest, NextResponse } from "next/server"; | ||
|
||
export async function POST( | ||
request: NextRequest, | ||
{ params }: { params: Promise<{ key: string }> } | ||
): Promise<NextResponse> { | ||
return handleApiRequestWithUser(async user => { | ||
if (getUserRole(user) !== roles.admin) { | ||
return NextResponse.json( | ||
{ message: "You do not have permission to rename this file." }, | ||
{ status: 403 } | ||
); | ||
} | ||
|
||
const { key } = await params; | ||
const file = await getFileById(key); | ||
if (!file) { | ||
return notFound; | ||
} | ||
|
||
const { id } = await request.json(); | ||
if (!id) { | ||
return NextResponse.json( | ||
{ message: "The id is required." }, | ||
{ status: 400 } | ||
); | ||
} | ||
|
||
if (id.length > env.FILE_ID_LENGTH) { | ||
return NextResponse.json( | ||
{ message: "The id is too long." }, | ||
{ status: 400 } | ||
); | ||
} | ||
|
||
if ((await getFileById(id)) !== undefined) { | ||
return NextResponse.json( | ||
{ message: "The id is already taken." }, | ||
{ status: 400 } | ||
); | ||
} | ||
|
||
// Rename the file | ||
await updateFile(file.id, { id: id }); | ||
await storage.renameFile( | ||
getFilePath(file.userId, file), | ||
getFilePath(file.userId, { | ||
...file, | ||
id: id, | ||
} as FileType) | ||
); | ||
|
||
// Rename the thumbnail if it exists | ||
if (file.hasThumbnail) { | ||
const thumbnail = await getThumbnailById(file.id); | ||
await storage.renameFile( | ||
getFileThumbnailPath(file.userId, thumbnail), | ||
getFileThumbnailPath(file.userId, { | ||
...thumbnail, | ||
id: id, | ||
} as ThumbnailType) | ||
); | ||
await updateThumbnail(file.id, { id: id }); | ||
} | ||
Logger.info( | ||
`The file ${getFileName(file)} had its id changed to ${id}.${file.extension} by ${user.username}` | ||
); | ||
|
||
return NextResponse.json( | ||
{ | ||
message: "Successfully renamed", | ||
}, | ||
{ status: 200 } | ||
); | ||
}); | ||
} |
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
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.