babyapi_file_upload_parser

package module
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 6, 2024 License: Apache-2.0 Imports: 14 Imported by: 0

README

go-chi decoder for http-multipart with some helper structure and functions designed to easily add file uploading support to BabyAPI

Usage

The example file is the best place to check for usage, and all the currently supported tested types and structures, but the basics are simple.

  1. Overritde the default decoder render.Decode = babyapiFileUploadParser.Decoder somewhere before starting your APIs
  2. Add a babyapiFileUploadParser.FileField field to your API struct (or a slice of them maybe a map, whatever you need)
  3. After create or update, write your files somewhere.
    • Call WriteFile(uploadPath string, id string) on each FileField item
    • Call babyapiFileUploadParser.WriteAllFileFields(uploadPath string, id string, api interface{}) on your api struct to write every FileField inside
  4. On delete ensure you clean up files

there is also a FileStore helper that helps implementing the needed hooks on the api to save and delete files along with providing a go-chi route that can be used for serving the files back. Here is an example of it's use

myFileStore := babyapiFileUploadParser.NewFileStore[*myResource](myAPI, "./UploadPath")
// using AutoAddHooks will automate writing files on create and update, and deleting
myFileStore.AutoAddHooks()
// Or you can add only the hooks you want
myAPI.SetAfterDelete(myFileStore.DeleteHook)
myAPI.SetOnCreateOrUpdate(myFileStore.CreateUpdateHook)
// Finally add the custom ID route to the myResource
myApi.AddCustomIDRoute(myFileStore.ServeFilesRoute("/files"))

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeMultipartForm

func DecodeMultipartForm(r *http.Request, v interface{})

func Decoder

func Decoder(r *http.Request, v interface{}) error

func DeleteAllFiles

func DeleteAllFiles(writePath string, id string, v interface{})

func WriteAllFileFields

func WriteAllFileFields(writePath string, id string, v interface{})

Types

type FileField

type FileField struct {
	FileName    string
	FileSize    int64
	MIMEHeader  textproto.MIMEHeader
	FormKey     string
	FormContext string
	// contains filtered or unexported fields
}

func FindByFileName

func FindByFileName(fileName string, v interface{}) (FileField, error)

func (*FileField) DeleteFile

func (ff *FileField) DeleteFile(fileStorePath string, id string) error

func (*FileField) GetFile

func (ff *FileField) GetFile(fileStorePath string, id string) (*os.File, error)

func (*FileField) GetFileDir

func (ff *FileField) GetFileDir(fileStorePath string, id string) string

func (*FileField) GetFileName

func (ff *FileField) GetFileName() string

func (*FileField) GetFilePath

func (ff *FileField) GetFilePath(fileStorePath string, id string) string

func (*FileField) HasContent

func (ff *FileField) HasContent() bool

func (*FileField) WriteFile

func (ff *FileField) WriteFile(fileStorePath string, id string) error

type FileStore

type FileStore[T babyapi.Resource] struct {
	FileStorePath string
	// contains filtered or unexported fields
}

func NewFileStore

func NewFileStore[T babyapi.Resource](api *babyapi.API[T], BaseFileStorePath string) FileStore[T]

func (FileStore[T]) AutoAddHooks

func (fs FileStore[T]) AutoAddHooks()

func (FileStore[T]) CreateUpdateHook

func (fs FileStore[T]) CreateUpdateHook(_ *http.Request, resource T) *babyapi.ErrResponse

func (FileStore[T]) DeleteHook

func (fs FileStore[T]) DeleteHook(r *http.Request) *babyapi.ErrResponse

func (FileStore[T]) DeleteResourceFiles

func (fs FileStore[T]) DeleteResourceFiles(id string) error

func (FileStore[T]) ServeFile

func (fs FileStore[T]) ServeFile(w http.ResponseWriter, r *http.Request) render.Renderer

func (FileStore[T]) ServeFilesRoute

func (fs FileStore[T]) ServeFilesRoute(basePattern string) chi.Route

type MultipartFormDecoder

type MultipartFormDecoder struct {
	// contains filtered or unexported fields
}

func (*MultipartFormDecoder) AddContext

func (d *MultipartFormDecoder) AddContext(formKey string) string

func (*MultipartFormDecoder) CheckDecodeNode

func (d *MultipartFormDecoder) CheckDecodeNode(formValue string, formKey string, tags []string, value reflect.Value)

func (*MultipartFormDecoder) DecodeNode

func (d *MultipartFormDecoder) DecodeNode(formValue string, formKey string, tags []string, value reflect.Value)

func (*MultipartFormDecoder) GetContext

func (d *MultipartFormDecoder) GetContext() string

func (*MultipartFormDecoder) GetFormKey

func (d *MultipartFormDecoder) GetFormKey(field reflect.StructField) (string, []string)

func (*MultipartFormDecoder) PopContext

func (d *MultipartFormDecoder) PopContext() string

func (*MultipartFormDecoder) PushContext

func (d *MultipartFormDecoder) PushContext(newLayer string)

func (*MultipartFormDecoder) RecursiveStructDecoder

func (d *MultipartFormDecoder) RecursiveStructDecoder(v reflect.Value)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL