Skip to content
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: object upload #121

Merged
merged 25 commits into from
Oct 15, 2024
Merged

feat: object upload #121

merged 25 commits into from
Oct 15, 2024

Conversation

matoszz
Copy link
Contributor

@matoszz matoszz commented Oct 7, 2024

  • Adds a pkg/objects to handle parsing of mulitpart forms and storage of objects to a backend (disk, s3 are supported currently)
  • Adds a middleware handler that can be used to parse the mutlipart forms (REST) and upload files to the backend (used by both REST and graph resolvers)
  • Adds a generic upload handler, this can be used to upload files but it won't associate those files to another other objects in the database, but could be associated later with the File resolver. This will updated to require object association in a follow-up PR that enforces permissions (along with the file graph resolver)
  • Adds the basic get cmds to the cli, waiting to to do the create/update until the file <-> object association is complete
  • Uses the Upload scalar for integration with graph, an example of this was added to the user schema:

Example Graph, add a avatar file:

(via apollo UI):

mutation UpdateUser($updateUserId: ID!, $input: UpdateUserInput!, $avatarFile: Upload) {
  updateUser(id: $updateUserId, input: $input, avatarFile: $avatarFile) {
    user {
      email
      firstName
      id
      files {
        id
        providedFileName
        md5Hash
        detectedMimeType
        detectedContentType
        storeKey
        uri
      }
      avatarLocalFileID
    }
  }
}

Request:

------WebKitFormBoundarysvy3AFmXBav3ErQx
Content-Disposition: form-data; name="operations"

{"query":"mutation UpdateUser($updateUserId: ID!, $input: UpdateUserInput!, $avatarFile: Upload) {\n  updateUser(id: $updateUserId, input: $input, avatarFile: $avatarFile) {\n    user {\n      email\n      firstName\n      id\n      files {\n        id\n        providedFileName\n        md5Hash\n        detectedMimeType\n        detectedContentType\n        storeKey\n        uri\n      }\n      avatarLocalFileID\n    }\n  }\n}","operationName":"UpdateUser","variables":{"updateUserId":"01JA3Z5PKPSB3QZ0XC9YPKFM2S","input":{"firstName":"Matt"},"avatarFile":null}}
------WebKitFormBoundarysvy3AFmXBav3ErQx
Content-Disposition: form-data; name="map"

{"0":["variables.avatarFile"]}
------WebKitFormBoundarysvy3AFmXBav3ErQx
Content-Disposition: form-data; name="0"; filename="file.png"
Content-Type: image/png


------WebKitFormBoundarysvy3AFmXBav3ErQx--

Response:

{
  "data": {
    "updateUser": {
      "user": {
        "email": "[email protected]",
        "firstName": "Matt",
        "id": "01JA3Z5PKPSB3QZ0XC9YPKFM2S",
        "files": [
          {
            "id": "01JA65YQBCEYTD8KRSAADXVG92",
            "providedFileName": "file.png",
            "md5Hash": "ap6SRa9K9WIYR8OOIJtshA==",
            "detectedMimeType": "image/png",
            "detectedContentType": "image/png",
            "storeKey": "avatarFile",
            "uri": "s3://openlane/01JA65YQBCEYTD8KRSAADXVG92_300x300.png"
          }
        ],
        "avatarLocalFileID": "01JA65YQBCEYTD8KRSAADXVG92"
      }
    }
  },
  "extensions": {
    "auth": {
      "authentication_type": "pat"
    },
    "server_latency": "23.927666ms",
    "trace_id": "EcZNCHdwScKOZMExwyDzLwnGUhTzJaqJ"
  }
}

or via the cli:

go run cmd/cli/main.go user update -a internal/graphapi/testdata/uploads/logo.png -i 01JA692ASHPGGTV7M3XQQXGADA  -z json
{
  "updateUser": {
    "user": {
      "authProvider": "CREDENTIALS",
      "avatarLocalFileID": "01JA7AF2B74ZVVG0XBT1F8VXSD",
...
go run cmd/cli/main.go file get -i 01JA7AF2B74ZVVG0XBT1F8VXSD -z json
{
  "file": {
    "categoryType": "",
    "createdAt": "2024-10-14T23:29:57.351558-06:00",
    "createdBy": "01JA692ASHPGGTV7M3XQQXGADA",
    "detectedContentType": "image/png",
    "detectedMimeType": "application/octet-stream",
    "id": "01JA7AF2B74ZVVG0XBT1F8VXSD",
    "md5Hash": "oDlWRQNn2le82pPXd1f6wA==",
    "persistedFileSize": 2041,
    "providedFileExtension": ".png",
    "providedFileName": "logo.png",
    "providedFileSize": 2041,
    "storagePath": "01JA7AF2B74ZVVG0XBT1F8VXSD_logo.png",
    "storageScheme": "s3://",
    "storageVolume": "openlane",
    "storeKey": "avatarFile",
    "updatedAt": "2024-10-14T23:29:57.425777-06:00",
    "updatedBy": "01JA692ASHPGGTV7M3XQQXGADA",
    "uri": "s3://openlane/01JA7AF2B74ZVVG0XBT1F8VXSD_logo.png"
  }
}

Example REST:

curl -s -H "Authorization: Bearer tolp_REDACTED"  -F [email protected]  localhost:17608/v1/upload
{
    "success": true,
    "message": "file(s) uploaded successfully",
    "file_count": 1,
    "files": [
      {
        "id": "01JA626FND96CC45WJA6W7991S",
        "name": "01JA626FND96CC45WJA6W7991S_file.png",
        "mime_type": "image/png",
        "presigned_url": "https://openlane.s3.us-east-2.amazonaws.com/01JA626FND96CC45WJA6W7991S_file.png?..."
      }
    ]
  }

Depends on: Yamashou/gqlgenc#237

// =========

type UploadFilesRequest struct {
UploadFile []byte `form:"uploadFile"`
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be byte or multipart.File

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think multipart.FileHeader, updating

pkg/objects/binder.go Outdated Show resolved Hide resolved
Signed-off-by: Sarah Funkhouser <[email protected]>
Signed-off-by: Sarah Funkhouser <[email protected]>
Signed-off-by: Sarah Funkhouser <[email protected]>
Signed-off-by: Sarah Funkhouser <[email protected]>
Signed-off-by: Sarah Funkhouser <[email protected]>
Signed-off-by: Sarah Funkhouser <[email protected]>
Signed-off-by: Sarah Funkhouser <[email protected]>
Signed-off-by: Sarah Funkhouser <[email protected]>
Signed-off-by: Sarah Funkhouser <[email protected]>
Copy link

sonarcloud bot commented Oct 15, 2024

Quality Gate Failed Quality Gate failed

Failed conditions
8.4% Duplication on New Code (required ≤ 5%)
C Security Rating on New Code (required ≥ A)

See analysis details on SonarCloud

Catch issues before they fail your Quality Gate with our IDE extension SonarLint

@golanglemonade golanglemonade merged commit 8820e20 into main Oct 15, 2024
17 of 18 checks passed
@golanglemonade golanglemonade deleted the feat-objectupload branch October 15, 2024 19:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants