-
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
648a891
commit 1b02c75
Showing
8 changed files
with
251 additions
and
231 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,40 @@ | ||
import { authError } from "@/lib/api-commons"; | ||
import { auth } from "@/lib/auth"; | ||
import { handleApiRequestWithUser } from "@/lib/api-commons"; | ||
import { env } from "@/lib/env"; | ||
import { ApiErrorResponse, ApiSuccessResponse } from "@/type/api/responses"; | ||
import { headers } from "next/headers"; | ||
import { NextResponse } from "next/server"; | ||
import { NextRequest, NextResponse } from "next/server"; | ||
|
||
export async function GET( | ||
request: Request | ||
): Promise<NextResponse<ApiSuccessResponse | ApiErrorResponse>> { | ||
const requestHeaders = await headers(); | ||
const session = await auth.api.getSession({ | ||
headers: requestHeaders, | ||
}); | ||
if (!session) { | ||
return authError; | ||
} | ||
export async function GET(request: NextRequest): Promise<NextResponse> { | ||
return handleApiRequestWithUser(async (user) => { | ||
if (!user.uploadToken) { | ||
return NextResponse.json( | ||
{ | ||
message: "You do not have an upload token", | ||
}, | ||
{ status: 400 } | ||
); | ||
} | ||
|
||
if (!session.user.uploadToken) { | ||
return NextResponse.json( | ||
JSON.stringify({ | ||
Version: "14.1.0", | ||
Name: `${env.NEXT_PUBLIC_WEBSITE_NAME}`, | ||
DestinationType: "ImageUploader, TextUploader, FileUploader", | ||
RequestMethod: "POST", | ||
RequestURL: `${env.NEXT_PUBLIC_WEBSITE_URL}/api/upload/sharex`, | ||
Body: "MultipartFormData", | ||
Arguments: { | ||
token: `${user.uploadToken}`, | ||
}, | ||
FileFormName: "sharex", | ||
URL: "{json:url}/{json:path}", | ||
DeletionURL: "{json:deletionUrl}", | ||
ErrorMessage: "{json:message}", | ||
}), | ||
{ | ||
message: "You do not have an upload token", | ||
}, | ||
{ status: 400 } | ||
status: 200, | ||
headers: { | ||
"Content-Disposition": `attachment; filename="sharex-config-${user.name}.sxcu"`, | ||
}, | ||
} | ||
); | ||
} | ||
|
||
return NextResponse.json( | ||
JSON.parse(`{ | ||
"Version": "14.1.0", | ||
"Name": "${env.NEXT_PUBLIC_WEBSITE_NAME}", | ||
"DestinationType": "ImageUploader, TextUploader, FileUploader", | ||
"RequestMethod": "POST", | ||
"RequestURL": "${env.NEXT_PUBLIC_WEBSITE_URL}/api/upload/sharex", | ||
"Body": "MultipartFormData", | ||
"Arguments": { | ||
"token": "${session.user.uploadToken}" | ||
}, | ||
"FileFormName": "sharex", | ||
"URL": "{json:url}/{json:path}", | ||
"DeletionURL": "{json:deletionUrl}", | ||
"ErrorMessage": "{json:message}" | ||
}`), | ||
{ | ||
status: 200, | ||
headers: { | ||
"Content-Disposition": `attachment; filename="sharex-config-${session.user.name}.sxcu"`, | ||
}, | ||
} | ||
); | ||
}); | ||
} |
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 |
---|---|---|
@@ -1,45 +1,34 @@ | ||
import { authError } from "@/lib/api-commons"; | ||
import { handleApiRequestWithUser } from "@/lib/api-commons"; | ||
import { auth } from "@/lib/auth"; | ||
import { UserType } from "@/lib/db/schemas/auth-schema"; | ||
import { generateUploadToken } from "@/lib/helpers/user"; | ||
import Logger from "@/lib/logger"; | ||
import { Notifications } from "@/lib/notification"; | ||
import { getUserPreferences } from "@/lib/preference"; | ||
import { ApiErrorResponse, ApiSuccessResponse } from "@/type/api/responses"; | ||
import { headers } from "next/headers"; | ||
import { NextResponse } from "next/server"; | ||
import { NextRequest, NextResponse } from "next/server"; | ||
|
||
export async function POST( | ||
request: Request | ||
): Promise<NextResponse<ApiSuccessResponse | ApiErrorResponse>> { | ||
const requestHeaders = await headers(); | ||
const session = await auth.api.getSession({ | ||
headers: requestHeaders, | ||
}); | ||
if (!session) { | ||
return authError; | ||
} | ||
const user = { | ||
...session.user, | ||
preferences: await getUserPreferences(session.user.id), | ||
} as UserType; | ||
export async function POST(request: NextRequest): Promise<NextResponse> { | ||
return handleApiRequestWithUser(async (user) => { | ||
const requestHeaders = await headers(); | ||
try { | ||
await auth.api.updateUser({ | ||
body: { | ||
uploadToken: generateUploadToken(), | ||
}, | ||
headers: requestHeaders, | ||
}); | ||
|
||
try { | ||
await auth.api.updateUser({ | ||
body: { | ||
uploadToken: generateUploadToken(), | ||
}, | ||
headers: requestHeaders, | ||
}); | ||
Notifications.sendResetUploadTokenNotification(user); | ||
} catch { | ||
return NextResponse.json( | ||
{ message: "An error occured when resetting the upload token" }, | ||
{ status: 500 } | ||
); | ||
} | ||
|
||
Notifications.sendResetUploadTokenNotification(user); | ||
} catch { | ||
Logger.info(`User ${user.username} reset their upload token`); | ||
return NextResponse.json( | ||
{ message: "An error occured when resetting the upload token" }, | ||
{ status: 500 } | ||
{ message: "Reset Upload Token" }, | ||
{ status: 200 } | ||
); | ||
} | ||
|
||
Logger.info(`User ${session.user.username} reset their upload token`); | ||
return NextResponse.json({ message: "Reset Upload Token" }, { status: 200 }); | ||
}); | ||
} |
Oops, something went wrong.