-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(backend, frontend): upload avatar #165
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
eaa222e
backend support for avatar upload
ZHallen122 12ec7d1
add windows support
ZHallen122 ba2832b
fix user setting not pop up when at root
ZHallen122 1a9848b
init avatar upload
ZHallen122 55fb445
update for user avatar
ZHallen122 fa5c56b
add avatar
ZHallen122 bbb1fb0
fix avatar is not render after up0load
ZHallen122 cc0e6b4
Support local media
ZHallen122 238d3a4
Fix fetch fail
ZHallen122 b0e1ee6
Merge remote-tracking branch 'origin/main' into feat-upload-avatar
ZHallen122 39f04c6
fix some ui problem but still have problem
ZHallen122 ccb923f
refactor: rename UserSettings component and update imports
Sma1lboy 51dfd57
feat: create settings page and refactor UserSettings component to Use…
Sma1lboy 791fdf0
Fix avatar not showing when on mac with s3
ZHallen122 90f9d52
[autofix.ci] apply automated fixes
autofix-ci[bot] 4c35014
delete log
ZHallen122 f6e9aa1
Merge branch 'feat-upload-avatar' of https://github.com/Sma1lboy/code…
ZHallen122 20b460d
fix error
ZHallen122 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Field, InputType } from '@nestjs/graphql'; | ||
import { FileUpload, GraphQLUpload } from 'graphql-upload-minimal'; | ||
|
||
@InputType() | ||
export class UploadAvatarInput { | ||
@Field(() => GraphQLUpload) | ||
file: Promise<FileUpload>; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import UserSetting from '@/components/settings/settings'; | ||
import { UserSettingsBar } from '@/components/user-settings-bar'; | ||
|
||
export default function Page() { | ||
return <UserSetting />; | ||
} |
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,64 @@ | ||
import { NextRequest } from 'next/server'; | ||
import fs from 'fs/promises'; // Use promises API | ||
import path from 'path'; | ||
import { getMediaDir } from 'codefox-common'; | ||
|
||
export async function GET( | ||
request: NextRequest, | ||
{ params }: { params: { path: string[] } } | ||
) { | ||
try { | ||
const mediaDir = getMediaDir(); | ||
const filePath = path.join(mediaDir, ...params.path); | ||
const normalizedPath = path.normalize(filePath); | ||
|
||
if (!normalizedPath.startsWith(mediaDir)) { | ||
console.error('Possible directory traversal attempt:', filePath); | ||
return new Response('Access denied', { status: 403 }); | ||
} | ||
|
||
// File extension allowlist | ||
const contentTypeMap: Record<string, string> = { | ||
'.jpg': 'image/jpeg', | ||
'.jpeg': 'image/jpeg', | ||
'.png': 'image/png', | ||
'.webp': 'image/webp', | ||
}; | ||
|
||
const ext = path.extname(filePath).toLowerCase(); | ||
if (!contentTypeMap[ext]) { | ||
return new Response('Forbidden file type', { status: 403 }); | ||
} | ||
|
||
// File existence and size check | ||
let fileStat; | ||
try { | ||
fileStat = await fs.stat(filePath); | ||
} catch (err) { | ||
return new Response('File not found', { status: 404 }); | ||
} | ||
|
||
if (fileStat.size > 10 * 1024 * 1024) { | ||
// 10MB limit | ||
return new Response('File too large', { status: 413 }); | ||
} | ||
|
||
// Read and return the file | ||
const fileBuffer = await fs.readFile(filePath); | ||
return new Response(fileBuffer, { | ||
headers: { | ||
'Content-Type': contentTypeMap[ext], | ||
'X-Content-Type-Options': 'nosniff', | ||
'Cache-Control': 'public, max-age=31536000', | ||
}, | ||
}); | ||
} catch (error) { | ||
console.error('Error serving media file:', error); | ||
const errorMessage = | ||
process.env.NODE_ENV === 'development' | ||
? `Error serving file: ${error.message}` | ||
: 'An error occurred while serving the file'; | ||
|
||
return new Response(errorMessage, { status: 500 }); | ||
} | ||
} |
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Verification agent
🧩 Analysis chain
Consistent implementation of media directory utilities.
The new media directory utility functions follow the established pattern in the codebase and use the existing
ensureDir
helper for creating directories when needed.One minor issue:
🏁 Script executed:
Length of output: 237
Media Directory Path Inconsistency – Action Needed
The new media directory utility functions are consistent overall, but there’s a minor discrepancy: the
getMediaPath
function usesgetModelsDir
instead of the expectedgetMediaDir
(as used ingetMediaAvatarsDir
). Please confirm whether this is intentional; if not, update the function to usegetMediaDir
for consistency with the media utilities pattern.Suggested diff:
📝 Committable suggestion