amplication icon indicating copy to clipboard operation
amplication copied to clipboard

⭐️ Plugin: Basic file upload

Open overbit opened this issue 2 years ago • 14 comments
trafficstars

This plugin will add some basic functionality to the generated service to allow file upload. As this plugin will be usable before we can release the full storage resource solution (https://github.com/amplication/amplication/issues/5488) some convention will need to be put in place to make this plugin possible.

When the plugin is installed, and the user adds a new entity field prefixed with file (i.e. fileLogo or fileImage) to an entity, the plugin will add the necessary additional methods (upload and download) to the entity. These methods will store or read the file to/from the filesystem and store/read the path from the database.

Requirements

  • The plugin will add upload and download methods to the entities with any field prefixed with file
  • The uploaded files will be stored in the filesystem in a customizable location by plugin settings
  • The plugin should throw an error if any field prefixed with file is not of type Json as it would not be able to store the necessary data
  • On file upload, the entity field file**** will be updated with an object containing information about the file
    {
      "fileName": "",
      "fileExtension": "",
      "filePath": ""
    }
    

Prize: USD 500 🏆

Reply with the proposed solutions and structures.

overbit avatar Sep 29 '23 08:09 overbit

@overbit @souravjain540 Can I work on it?

ptdatta avatar Sep 30 '23 02:09 ptdatta

@ptdatta this doesn't even have description as of now/

souravjain540 avatar Oct 01 '23 03:10 souravjain540

File Upload Approach

Hi, I would love to work on this idea and hopefully help a lot of developers in this (sometimes tricky) process of file uploading/downloading

  1. Try out the idea via modifying a generated service and controller
  2. After following the docs about Plugin development,(https://docs.amplication.com/plugins/overview/, which I’ve read) create a new plugin using the template
  3. Events to take care:
    1. BEFORE: CreateServerPackageJson - Installed the required packages*
    2. BEFORE: CreateServer - Check if the field is a type json then only proceed or else through an error, and check the if the settings has the file path or if it’s empty use the default something as public/files
    3. BEFORE: CreateEntityServiceBase - in this we’ll first add the required things such as
    4. as well as many others.. such as to
      1. Adding controller (+base) for the file uploading+adding the url to the database to the entity field(which would required to change the entity schema and adding an additional field with field***URL(path from /src?) + modifying the entity field with the above given json(filename, extension, path), serving of the uploaded static files(downloading)
      2. I don’t think there would be any need to modify the user resolver..
      3. Add the controller to the ControllerToMany relations
      4. Adding some file data to the CreateSeed would be helpful to add some dummy static data which can be stored as some images/pdf to try out for the user.
      5. One thing I forgot to add was the updating Admin UI which is also crucial for the (either displaying) or directly downloading the files
  4. Make those controllers to expose the {entityName}/upload and {entityName}/download routes
  5. I’ve found a great reference to this would be the auth-core plugin

* Can I used a third party package like multer (which is given by inbuild by nestjs with nestjs/platform-express ) or handling the whole file uploading part without it? (using the available methods in nodejs)

This looks (and is) like a big task to do.. but doing it in steps and after breaking down into smaller chunks would help get this to completion!

I think I still might be missing somethings, which hopefully can be figured along the way.. Open to suggestions! Thank you 😃

References

  1. https://github.com/amplication/plugins/blob/f00090f82f979bd93804c8380197f9edd73c053b/plugins/auth-core
  2. https://stackoverflow.com/questions/49096068/upload-file-using-nestjs-and-multer
  3. https://docs.nestjs.com/techniques/file-upload
  4. https://docs.nestjs.com/recipes/serve-static

codingmickey avatar Oct 03 '23 18:10 codingmickey

@souravjain540 @overbit Can I work on it?

AAKASHBANSAL96 avatar Oct 05 '23 06:10 AAKASHBANSAL96

@codingmickey assigned to you, please dm me on our discord and i will open a pvt channel for us.

souravjain540 avatar Oct 05 '23 13:10 souravjain540

Hi, I have read the documentation at https://docs.amplication.com/plugins/overview/ and would like to work on this plugin, Please assign me this.

Task List:

  • [x] Plugin Initialization
  • [ ] Custom File Fields
  • [ ] Admin UI Components
  • [ ] File Validation
  • [ ] Storage Integration
  • [ ] Controller and Routes Setup
  • [ ] File URL Generation

basedavishkar avatar Nov 09 '23 23:11 basedavishkar

Hey @ashkaaar I'm currently working on this plugin and it's almost near completion.. so I hope you find other plugin to work on! :)

codingmickey avatar Nov 09 '23 23:11 codingmickey

oh, that's fine

basedavishkar avatar Nov 10 '23 00:11 basedavishkar

is anyone still working on it

PentesterPriyanshu avatar Nov 20 '23 12:11 PentesterPriyanshu

Yes, I'm working on this and there are some linked issues with changes in the dsg-generator

codingmickey avatar Nov 20 '23 13:11 codingmickey

Some internal comments for us to consider as part of the longer-term design:

  • Consider instead of JSON field and special field name, adding a special field type called "File"
  • This new field type can have its own unique properties such as allowed extensions, min/max file size, etc.
  • This new field type should have a property for "provider" so that I can use multiple storage providers (each is a plugin) on the same service - different fields will be able to use different storage providers (e.g. some files are from local FS, some from S3, etc.)
  • Each field of type "File", will be translated to a DB field type by the storage provider it is mapped to. We can start by default with JSON.

In addition, we need to consider things like:

  • Upload/download that flows through the generated server, VS. upload/download that uses a token to an external storage provider (that supports API)
  • Authorization will be based on the current RBAC approach that exists in Amplication for entire entities / specific fields. In the future, if we implement ABAC/RLS , it will apply automatically by the authz decorators

@PazYanoverr

mulygottlieb avatar Nov 26 '23 10:11 mulygottlieb

  • Consider instead of JSON field and special field name, adding a special field type called "File"
  • This new field type can have its own unique properties such as allowed extensions, min/max file size, etc.

Was thinking the same(https://discord.com/channels/757179260417867879/1159741418886139934/1177012278667788288, as if not this then this would become a native feature and not an independent plugin) but then thought the basic version will be necessary for the above I'll raise a draft PR and then I'll work on doing this

codingmickey avatar Nov 26 '23 12:11 codingmickey

Thanks @codingmickey. Sounds good. Let us know if you need help or clarification with any of the above.

@overbit FYI

mulygottlieb avatar Nov 27 '23 10:11 mulygottlieb

@codingmickey - Any updates regarding the plugin?

PazYanoverr avatar Jan 31 '24 14:01 PazYanoverr

https://github.com/amplication/private-issues/issues/132

PazYanoverr avatar Mar 18 '24 09:03 PazYanoverr