-
-
Notifications
You must be signed in to change notification settings - Fork 24
Added Google Cloud Storage (GCS) integration for image uploads #533
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
Merged
Merged
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
01642b9
Added handler for uploading file to gcp
vershwal 8f16d05
Added Validations to storage.ts
vershwal de6ff91
Updated tests
vershwal 67039db
Fack gcp works
vershwal 69530a6
Perfect code
vershwal 9156d2e
Fixed tests
vershwal 1769042
Fixed tests
vershwal 66b7b55
Fixed lint
vershwal 0fb8df9
Merge branch 'main' into gcpStorage
vershwal 1fd2e78
Merge branch 'main' into gcpStorage
vershwal bfdea40
Added bucket check on boot and updated tests
vershwal 82300fb
Fixed tests
vershwal 23731e2
Updated tests
vershwal f111355
Initialized bucket on boot
vershwal a306024
Updated code
vershwal 3db475b
Added gcp storage initialisation to GCPStorageService
vershwal 06a1d0a
Moved storage.ts to src/http, and used storage service as a dependency
vershwal f5694d3
Fixed lint errors
vershwal 16bf503
Added logs
vershwal 201308a
Fixed lint
vershwal 45bd52c
Added more logs
vershwal 7fc468f
Fixed tests
vershwal d5a793d
Removed logging from gcp-service
vershwal a62e584
Merge branch 'main' into gcpStorage
vershwal c7470ea
Set up bucket creation for fake-gcs
vershwal c089b58
Merge branch 'main' into gcpStorage
vershwal 4fa0908
Added default error handling
vershwal 1999c50
Added log
vershwal e1d1067
Added log for testing
vershwal 648093e
Setting config in constructor rather than read from the environment v…
vershwal 5cd30c4
Added blank line in script
vershwal 19d833a
Merge branch 'main' into gcpStorage
vershwal 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -75,3 +75,6 @@ typings/ | |
| dist/ | ||
|
|
||
| cedar/data-generation/output/* | ||
|
|
||
| # Fake GCS data | ||
| fake-gcs/storage* | ||
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,11 @@ | ||
| FROM fsouza/fake-gcs-server | ||
|
|
||
| # Install curl | ||
| RUN apk add --no-cache curl | ||
|
|
||
| # Copy the initialization script | ||
| COPY start.sh /start.sh | ||
| RUN chmod +x /start.sh | ||
|
|
||
| # Set the entrypoint | ||
| ENTRYPOINT ["/start.sh"] |
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,25 @@ | ||
| #!/bin/sh | ||
|
|
||
| # This script initializes the fake-gcs server and ensures the required bucket exists. | ||
| # It starts the server, waits for it to be ready, and creates the bucket if it doesn't exist. | ||
|
|
||
| # Ensure storage directory exists and has proper permissions | ||
| mkdir -p /storage | ||
| chmod 777 /storage | ||
|
|
||
| # Start the fake-gcs server in the background | ||
| fake-gcs-server -scheme http -port 4443 -external-url http://fake-gcs:4443 -data /storage & | ||
|
|
||
| # Wait for the server to be ready | ||
| sleep 1 | ||
|
|
||
| # Check if bucket exists and create if it doesn't | ||
| if ! curl -s "http://localhost:4443/storage/v1/b/${GCP_BUCKET_NAME}" | grep -q "\"name\": \"${GCP_BUCKET_NAME}\""; then | ||
| curl -X POST \ | ||
| -H "Content-Type: application/json" \ | ||
| -d "{\"name\": \"${GCP_BUCKET_NAME}\"}" \ | ||
| "http://localhost:4443/storage/v1/b?project=${GCP_PROJECT_ID}" | ||
| fi | ||
|
|
||
| # Keep the container running | ||
| tail -f /dev/null |
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,8 @@ | ||
| Feature: Image Upload API | ||
| As an authenticated user | ||
| I want to upload images | ||
|
|
||
| Scenario: Upload an image | ||
| When an authenticated "post" request is made to "/.ghost/activitypub/upload/image" with a file | ||
| Then the request is accepted with a 200 | ||
| And the response contains a file URL |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import type { AccountService } from 'account/account.service'; | ||
| import { exhaustiveCheck, getError, getValue, isError } from 'core/result'; | ||
| import type { Context } from 'hono'; | ||
| import type { GCPStorageService } from 'storage/gcloud-storage/gcp-storage.service'; | ||
|
|
||
| export function createStorageHandler( | ||
| accountService: AccountService, | ||
| storageService: GCPStorageService, | ||
| ) { | ||
| /** | ||
| * Handle an upload to GCloud Storage bucket | ||
| */ | ||
| return async function handleUpload(ctx: Context) { | ||
| const logger = ctx.get('logger'); | ||
| const formData = await ctx.req.formData(); | ||
| const file = formData.get('file'); | ||
|
|
||
| if (!file || !(file instanceof File)) { | ||
| return new Response('No valid file provided', { status: 400 }); | ||
| } | ||
|
|
||
| const account = await accountService.getAccountForSite(ctx.get('site')); | ||
| const result = await storageService.saveFile(file, account.uuid); | ||
vershwal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if (isError(result)) { | ||
| const error = getError(result); | ||
| switch (error) { | ||
| case 'file-too-large': | ||
| logger.error(`File is too large: ${file.size} bytes`); | ||
| return new Response('File is too large', { status: 413 }); | ||
| case 'file-type-not-supported': | ||
| logger.error(`File type ${file.type} is not supported`); | ||
| return new Response( | ||
| `File type ${file.type} is not supported`, | ||
| { status: 415 }, | ||
| ); | ||
| default: | ||
| exhaustiveCheck(error); | ||
| } | ||
| } | ||
|
|
||
| const fileUrl = getValue(result); | ||
| return new Response(JSON.stringify({ fileUrl }), { | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| status: 200, | ||
| }); | ||
| }; | ||
| } | ||
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.
I can make this tests in a separate file like we discussed yesterday.