({ request, user }: RequestHandlerParams)
| 20 | } |
| 21 | |
| 22 | async function post({ request, user }: RequestHandlerParams) { |
| 23 | if (!(await AppConfig.isAppEnabled('files'))) { |
| 24 | return new Response('Forbidden', { status: 403 }); |
| 25 | } |
| 26 | |
| 27 | const isPublicFileSharingAllowed = await AppConfig.isPublicFileSharingAllowed(); |
| 28 | |
| 29 | if (!isPublicFileSharingAllowed) { |
| 30 | return new Response('Forbidden', { status: 403 }); |
| 31 | } |
| 32 | |
| 33 | const requestBody = await request.clone().json() as RequestBody; |
| 34 | |
| 35 | if ( |
| 36 | !requestBody.filePath || !requestBody.pathInView || !requestBody.filePath.trim() || |
| 37 | !requestBody.pathInView.trim() |
| 38 | ) { |
| 39 | return new Response('Bad Request', { status: 400 }); |
| 40 | } |
| 41 | |
| 42 | // Fix Windows clients sending the directory path with backslashes |
| 43 | requestBody.filePath = requestBody.filePath.replace(/\\/g, '/'); |
| 44 | |
| 45 | if ( |
| 46 | !requestBody.filePath.startsWith('/') || |
| 47 | requestBody.filePath.includes('../') || !requestBody.pathInView.startsWith('/') || |
| 48 | requestBody.pathInView.includes('../') |
| 49 | ) { |
| 50 | return new Response('Bad Request', { status: 400 }); |
| 51 | } |
| 52 | |
| 53 | // Confirm the file path belongs to the user |
| 54 | const { isDirectory, isFile } = await getPathInfo( |
| 55 | user!.id, |
| 56 | requestBody.filePath, |
| 57 | ); |
| 58 | |
| 59 | // Confirm the file path ends with a / if it's a directory, and doesn't end with a / if it's a file |
| 60 | if (isDirectory && !requestBody.filePath.endsWith('/')) { |
| 61 | requestBody.filePath = `${requestBody.filePath}/`; |
| 62 | } else if (isFile && requestBody.filePath.endsWith('/')) { |
| 63 | requestBody.filePath = requestBody.filePath.slice(0, -1); |
| 64 | } |
| 65 | |
| 66 | const extra: FileShare['extra'] = {}; |
| 67 | |
| 68 | if (requestBody.password) { |
| 69 | extra.hashed_password = await generateHash(`${requestBody.password}:${PASSWORD_SALT}`, 'SHA-256'); |
| 70 | } |
| 71 | |
| 72 | const fileShare: Omit<FileShare, 'id' | 'created_at'> = { |
| 73 | user_id: user!.id, |
| 74 | file_path: requestBody.filePath, |
| 75 | extra, |
| 76 | }; |
| 77 | |
| 78 | const createdFileShare = await FileShareModel.create(fileShare); |
| 79 |
nothing calls this directly
no test coverage detected