Skip to content

Commit

Permalink
Update FAQ and restructure readme (#20)
Browse files Browse the repository at this point in the history
* update readme with faqs

* fix markdown lint

* add generic readertoseeker implmentation

* fix seeker

* add reader
  • Loading branch information
adelowo authored Sep 21, 2024
1 parent a17ea85 commit 10ada24
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 8 deletions.
24 changes: 17 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,18 @@ for your web apps. It follows the standard
`http.Handler` and `http.HandlerFunc` interfaces so you can
always use with any of framework or the standard library router.

Multiple files per form field are already supported

> Name and idea was gotten from the insanely popular multer package
> in NodeJS that does the same.
> [!NOTE]
> Name and idea was gotten from the insanely popular multer package in NodeJS that does the same
- [Installation](#installation)
- [Usage](#usage)
- [Standard HTTP router](#standard-http-router)
- [Chi Router and others](#chi-router-and-other-compatible-http-handlers)
- [API](#api)
- [FAQs](#faqs)
- [customizing http error](#customizing-the-error-response)
- [ignoring keys](#ignoring-non-existent-keys-in-the-multipart-request)
- [custom validation logic](#writing-your-custom-validator-logic)

## Installation

Expand Down Expand Up @@ -172,15 +180,17 @@ Gulter also ships with two storage implementations at the moment:
- `DiskStore`: uses a local filesystem backed store to upload files
- `CloudinaryStore`: uploads file to cloudinary
## Ignoring non existent keys in the multipart Request
## FAQs
### Ignoring non existent keys in the multipart Request
Sometimes, the keys you have configured the middleware might get dropped from the
frontend for some reason, ideally the middleware fails if it cannot find a
configured key in the request. To disable this behavior and ignore the missing
key, you can make use of the `WithIgnoreNonExistentKey(true)` option to prevent the
middleware from causing an error when such keys do not exists
## Customizing the error response
### Customizing the error response
Since Gulter is a middleware that runs, it returns an error to the client if found,
this might not match your existing structure, so to configure the response, use the
Expand All @@ -198,7 +208,7 @@ to define yours.
}
```
## Writing your custom validator logic
### Writing your custom validator logic
Sometimes, you could have some custom logic to validate uploads, in this example
below, we limit the size of the upload based on the mimeypes of the uploaded files
Expand Down
31 changes: 31 additions & 0 deletions reader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package gulter

import (
"io"
"os"
)

func ReaderToSeeker(r io.Reader) (io.ReadSeeker, error) {
tmpfile, err := os.CreateTemp("", "upload-")
if err != nil {
return nil, err
}

_, err = io.Copy(tmpfile, r)
if err != nil {
_ = tmpfile.Close()
_ = os.Remove(tmpfile.Name())
return nil, err
}

_, err = tmpfile.Seek(0, 0)
if err != nil {
_ = tmpfile.Close()
_ = os.Remove(tmpfile.Name())
return nil, err
}

// Return the file, which implements io.ReadSeeker
// which you can now pass to the gulter uploader
return tmpfile, nil
}
8 changes: 7 additions & 1 deletion storage/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func (s *S3Store) Close() error { return nil }
func (s *S3Store) Upload(ctx context.Context, r io.Reader,
opts *gulter.UploadFileOptions,
) (*gulter.UploadedFileMetadata, error) {

b := new(bytes.Buffer)

r = io.TeeReader(r, b)
Expand All @@ -97,12 +98,17 @@ func (s *S3Store) Upload(ctx context.Context, r io.Reader,
return nil, err
}

seeker, err := gulter.ReaderToSeeker(b)
if err != nil {
return nil, err
}

_, err = s.client.PutObject(ctx, &s3.PutObjectInput{
Bucket: aws.String(s.opts.Bucket),
Metadata: opts.Metadata,
Key: aws.String(opts.FileName),
ACL: s.opts.ACL,
Body: b,
Body: seeker,
})
if err != nil {
return nil, err
Expand Down

0 comments on commit 10ada24

Please sign in to comment.