-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Refactor file upload and toast notifications #472
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
Open
vincanger
wants to merge
15
commits into
main
Choose a base branch
from
refactor-file-upload-and-toast-notifications
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
911461b
Refactor file upload and toast notifications
vincanger 0317b06
Enforce file upload limit and update dependencies on opensaas-sh
vincanger f56de23
update tests
vincanger e91a93b
Improve file deletion error handling and cleanup
vincanger 5013b48
Add credit check and S3 file existence validation
vincanger 0815fd6
Update s3Utils.ts
vincanger 16bb3c5
update app_diff
vincanger 7bd0bbc
Merge branch 'main' into refactor-file-upload-and-toast-notifications
vincanger 605220d
fix diff
vincanger 2f1117d
Update deletions
vincanger 4f94475
Improve toast UI, error handling, and credit messaging
vincanger 710ea97
Refactor file upload validation and error handling
vincanger 2d2b2d0
Refactor file upload to use s3Key and add cleanup job
vincanger 165b678
add orphaned file clean up
vincanger 1e28ade
remove s3 cleanup job from template
vincanger 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 hidden or 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
11 changes: 11 additions & 0 deletions
11
opensaas-sh/app_diff/migrations/20250731133938_drop_upload_url_from_file/migration.sql.diff
This file contains hidden or 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,11 @@ | ||
--- template/app/migrations/20250731133938_drop_upload_url_from_file/migration.sql | ||
+++ opensaas-sh/app/migrations/20250731133938_drop_upload_url_from_file/migration.sql | ||
@@ -0,0 +1,8 @@ | ||
+/* | ||
+ Warnings: | ||
+ | ||
+ - You are about to drop the column `uploadUrl` on the `File` table. All the data in the column will be lost. | ||
+ | ||
+*/ | ||
+-- AlterTable | ||
+ALTER TABLE "File" DROP COLUMN "uploadUrl"; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,24 @@ | ||
--- template/app/src/client/App.tsx | ||
+++ opensaas-sh/app/src/client/App.tsx | ||
@@ -4,6 +4,7 @@ | ||
import './Main.css'; | ||
import NavBar from './components/NavBar/NavBar'; | ||
import { demoNavigationitems, marketingNavigationItems } from './components/NavBar/constants'; | ||
+import { useIsLandingPage } from './hooks/useIsLandingPage'; | ||
import CookieConsentBanner from './components/cookie-consent/Banner'; | ||
import { Toaster } from '../components/ui/toaster'; | ||
|
||
@@ -13,11 +14,8 @@ | ||
*/ | ||
export default function App() { | ||
const location = useLocation(); | ||
- const isMarketingPage = useMemo(() => { | ||
- return location.pathname === '/' || location.pathname.startsWith('/pricing'); | ||
- }, [location]); | ||
- | ||
- const navigationItems = isMarketingPage ? marketingNavigationItems : demoNavigationitems; | ||
+ const isLandingPage = useIsLandingPage(); | ||
+ const navigationItems = isLandingPage ? marketingNavigationItems : demoNavigationitems; | ||
|
||
const shouldDisplayAppNavBar = useMemo(() => { | ||
return ( |
8 changes: 7 additions & 1 deletion
8
opensaas-sh/app_diff/src/client/components/NavBar/constants.ts.diff
This file contains hidden or 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,10 +1,16 @@ | ||
--- template/app/src/client/components/NavBar/constants.ts | ||
+++ opensaas-sh/app/src/client/components/NavBar/constants.ts | ||
@@ -9,7 +9,6 @@ | ||
@@ -9,12 +9,12 @@ | ||
|
||
export const marketingNavigationItems: NavigationItem[] = [ | ||
{ name: 'Features', to: '/#features' }, | ||
- { name: 'Pricing', to: routes.PricingPageRoute.to }, | ||
...staticNavigationItems, | ||
] as const; | ||
|
||
export const demoNavigationitems: NavigationItem[] = [ | ||
{ name: 'AI Scheduler', to: routes.DemoAppRoute.to }, | ||
{ name: 'File Upload', to: routes.FileUploadRoute.to }, | ||
+ { name: 'Pricing', to: routes.PricingPageRoute.to }, | ||
...staticNavigationItems, | ||
] as const; |
13 changes: 13 additions & 0 deletions
13
opensaas-sh/app_diff/src/file-upload/FileUploadPage.tsx.diff
This file contains hidden or 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,13 @@ | ||
--- template/app/src/file-upload/FileUploadPage.tsx | ||
+++ opensaas-sh/app/src/file-upload/FileUploadPage.tsx | ||
@@ -144,8 +144,8 @@ | ||
</h2> | ||
</div> | ||
<p className='mx-auto mt-6 max-w-2xl text-center text-lg leading-8 text-muted-foreground'> | ||
- This is an example file upload page using AWS S3. Maybe your app needs this. Maybe it doesn't. But a | ||
- lot of people asked for this feature, so here you go 🤝 | ||
+ This is an example file upload page using AWS S3. Maybe your app needs this. Maybe it doesn't. But | ||
+ a lot of people asked for this feature, so here you go 🤝 | ||
</p> | ||
<Card className='my-8'> | ||
<CardContent className='space-y-10 my-10 py-8 px-4 mx-auto sm:max-w-lg'> |
26 changes: 23 additions & 3 deletions
26
opensaas-sh/app_diff/src/file-upload/fileUploading.ts.diff
This file contains hidden or 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,9 +1,29 @@ | ||
--- template/app/src/file-upload/fileUploading.ts | ||
+++ opensaas-sh/app/src/file-upload/fileUploading.ts | ||
@@ -1,5 +1,5 @@ | ||
-import { createFile } from 'wasp/client/operations'; | ||
@@ -1,5 +1,7 @@ | ||
+import type { User } from 'wasp/entities'; | ||
import axios from 'axios'; | ||
+import { createFile } from 'wasp/client/operations'; | ||
import { ALLOWED_FILE_TYPES, MAX_FILE_SIZE_BYTES } from './validation'; | ||
+import { PrismaClient } from '@prisma/client'; | ||
|
||
export type FileWithValidType = Omit<File, 'type'> & { type: AllowedFileType }; | ||
type AllowedFileType = (typeof ALLOWED_FILE_TYPES)[number]; | ||
@@ -63,3 +65,17 @@ | ||
function isAllowedFileType(fileType: string): fileType is AllowedFileType { | ||
return (ALLOWED_FILE_TYPES as readonly string[]).includes(fileType); | ||
} | ||
+ | ||
+export async function checkIfUserHasReachedFileUploadLimit({ userId, prismaFileDelegate }: { userId: User['id']; prismaFileDelegate: PrismaClient['file'] }) { | ||
+ const numberOfFilesByUser = await prismaFileDelegate.count({ | ||
+ where: { | ||
+ user: { | ||
+ id: userId, | ||
+ }, | ||
+ }, | ||
+ }); | ||
+ if (numberOfFilesByUser >= 2) { | ||
+ return true; | ||
+ } | ||
+ return false; | ||
+} | ||
\ No newline at end of file |
This file contains hidden or 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,39 +1,34 @@ | ||
--- template/app/src/file-upload/operations.ts | ||
+++ opensaas-sh/app/src/file-upload/operations.ts | ||
@@ -1,14 +1,14 @@ | ||
@@ -1,6 +1,5 @@ | ||
-import * as z from 'zod'; | ||
-import { HttpError } from 'wasp/server'; | ||
import { type File } from 'wasp/entities'; | ||
+import { HttpError } from 'wasp/server'; | ||
import { | ||
type CreateFile, | ||
type GetAllFilesByUser, | ||
type GetDownloadFileSignedURL, | ||
@@ -8,6 +7,8 @@ | ||
type CreateFileUploadUrl, | ||
type AddFileToDb, | ||
} from 'wasp/server/operations'; | ||
+import { checkIfUserHasReachedFileUploadLimit } from './fileUploading'; | ||
+import * as z from 'zod'; | ||
|
||
-import { getUploadFileSignedURLFromS3, getDownloadFileSignedURLFromS3 } from './s3Utils'; | ||
import { getUploadFileSignedURLFromS3, getDownloadFileSignedURLFromS3, deleteFileFromS3 } from './s3Utils'; | ||
import { ensureArgsSchemaOrThrowHttpError } from '../server/validation'; | ||
+import { getDownloadFileSignedURLFromS3, getUploadFileSignedURLFromS3 } from './s3Utils'; | ||
import { ALLOWED_FILE_TYPES } from './validation'; | ||
@@ -32,6 +33,14 @@ | ||
throw new HttpError(401); | ||
} | ||
|
||
const createFileInputSchema = z.object({ | ||
@@ -37,6 +37,18 @@ | ||
userId: context.user.id, | ||
}); | ||
|
||
+ const numberOfFilesByUser = await context.entities.File.count({ | ||
+ where: { | ||
+ user: { | ||
+ id: context.user.id, | ||
+ }, | ||
+ }, | ||
+ const userFileLimitReached = await checkIfUserHasReachedFileUploadLimit({ | ||
+ userId: context.user.id, | ||
+ prismaFileDelegate: context.entities.File, | ||
+ }); | ||
+ | ||
+ if (numberOfFilesByUser >= 2) { | ||
+ throw new HttpError(403, 'Thanks for trying Open SaaS. This demo only allows 2 file uploads per user.'); | ||
+ if (userFileLimitReached) { | ||
+ throw new HttpError(403, 'This demo only allows 2 file uploads per user.'); | ||
+ } | ||
+ | ||
await context.entities.File.create({ | ||
data: { | ||
name: fileName, | ||
const { fileType, fileName } = ensureArgsSchemaOrThrowHttpError(createFileInputSchema, rawArgs); | ||
|
||
const { s3UploadUrl, s3UploadFields, key } = await getUploadFileSignedURLFromS3({ |
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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.
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.
Uh oh!
There was an error while loading. Please reload this page.