uploader

package
v0.0.0-...-c407d37 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// FileTypeJPEG is the content type for JPEG images
	FileTypeJPEG = "image/jpeg"
	// FileTypePNG is the content type for PNG images
	FileTypePNG = "image/png"
	// FileTypePDF is the content type for PDF documents
	FileTypePDF = "application/pdf"
	// FileTypeText is the content type for text files
	FileTypeText = "text/plain"
	// FileTypeTextUTF8 is the content type for text files with UTF-8 encoding
	FileTypeTextUTF8 = "text/plain; charset=utf-8"
	// FileTypeExcel is the content type for Excel files
	FileTypeExcel = "application/vnd.ms-excel"
	// FileTypeExcelXLSX is the content type for Excel xlsx files
	FileTypeExcelXLSX = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
)
View Source
const (
	// B Byte
	B ByteSize = 1
	// KB KiloByte
	KB = 1000
	// MB MegaByte
	MB = 1000 * 1000
)
View Source
const AccessiblePDFFormat = "PDF/A-1a"
View Source
const MaxCustomerUserUploadFileSizeLimit = 25 * MB

MaxCustomerUserUploadFileSizeLimit sets the maximum file size limit for a logged in customer user file upload. The 25MB limit was intended to apply to individual document uploads (customer UI). If working with the office UI, the office users are expected to generate PDFs of combined documents which can exceed this limit.

View Source
const MaxFileSizeLimit = 250 * MB

MaxFileSizeLimit sets the maximum file size limit Anti-Virus scanning won't be able to scan files larger than 250MB Any unscanned files will not be available for download so while we can upload a larger file of any size the file will be locked from downloading forever.

View Source
const MaxOfficeUploadFileSizeLimit = 100 * MB

MaxOfficeUploadFileSizeLimit sets the maximum file size limit for a logged in office generated file. Office users can generate PDFs of combined documents with a max size of 100MB

Variables

View Source
var ErrFileSizeLimitExceedsMax = errors.Errorf("FileSizeLimit exceeds max of %d bytes", MaxFileSizeLimit)

ErrFileSizeLimitExceedsMax is an error where file size exceeds max size

View Source
var ErrZeroLengthFile = errors.New("File has length of 0")

ErrZeroLengthFile represents an error caused by a file with no content

Functions

func CreateUserUploadForDocumentWrapper

func CreateUserUploadForDocumentWrapper(
	appCtx appcontext.AppContext, userID uuid.UUID,
	storer storage.FileStorer, file io.ReadCloser,
	filename string,
	fileSizeLimit ByteSize,
	allowedFileTypes AllowedFileTypes,
	docID *uuid.UUID,
) (*models.UserUpload, string, *validate.Errors, error)

CreateUserUploadForDocumentWrapper wrapper/helper function to create a user upload

Types

type AllowedFileTypes

type AllowedFileTypes []string

AllowedFileTypes contains a list of content types

var (
	// AllowedTypesServiceMember are the content types we allow service members to upload for orders
	AllowedTypesServiceMember AllowedFileTypes = []string{FileTypeJPEG, FileTypePNG, FileTypePDF}

	// AllowedTypesPPMDocuments are the content types we allow service members to upload for PPM shipment closeout documentation
	AllowedTypesPPMDocuments AllowedFileTypes = []string{FileTypeJPEG, FileTypePNG, FileTypePDF, FileTypeExcel, FileTypeExcelXLSX}

	// AllowedTypesPaymentRequest are the content types we allow prime to upload
	AllowedTypesPaymentRequest AllowedFileTypes = []string{FileTypeJPEG, FileTypePNG, FileTypePDF}

	// AllowedTypesServiceRequest are the content types we allow prime to upload for proof of service item requests
	AllowedTypesServiceRequest AllowedFileTypes = []string{FileTypeJPEG, FileTypePNG, FileTypePDF}

	// AllowedTypesText accepts text files
	AllowedTypesText AllowedFileTypes = []string{FileTypeText, FileTypeTextUTF8}

	// AllowedTypesPDF accepts PDF files
	AllowedTypesPDF AllowedFileTypes = []string{FileTypePDF}

	// AllowedTypesPDFImages accepts PDF files and images
	AllowedTypesPDFImages AllowedFileTypes = []string{FileTypeJPEG, FileTypePNG, FileTypePDF}

	// AllowedTypesAny accepts any file type
	AllowedTypesAny AllowedFileTypes = []string{"*"}
)

func (AllowedFileTypes) Contains

func (aft AllowedFileTypes) Contains(fileType string) bool

Contains checks to see if the provided file type is acceptable

func (AllowedFileTypes) Contents

func (aft AllowedFileTypes) Contents() []string

Contents returns the allowed types as a slice of strings

type ByteSize

type ByteSize int64

ByteSize is a snack

func (ByteSize) Int64

func (b ByteSize) Int64() int64

Int64 returns an integer of the byte size

type ErrFailedToInitUploader

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

ErrFailedToInitUploader represents an error creating a new file uploader

func (ErrFailedToInitUploader) Error

func (e ErrFailedToInitUploader) Error() string

ErrFailedToInitUploader is the string representation of an error

type ErrFile

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

ErrFile represents an file error

func (ErrFile) Error

func (e ErrFile) Error() string

ErrFile is the string representation of an error

type ErrTooLarge

type ErrTooLarge struct {
	FileSize      int64
	FileSizeLimit ByteSize
}

ErrTooLarge is an error where the file size exceeds the limit

func (ErrTooLarge) Error

func (e ErrTooLarge) Error() string

ErrTooLarge is the string representation of an error

type ErrUnsupportedContentType

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

ErrUnsupportedContentType represents an error caused by a file with a non-allowed content type

func NewErrUnsupportedContentType

func NewErrUnsupportedContentType(contentType string, allowedContentTypes []string) ErrUnsupportedContentType

func (ErrUnsupportedContentType) Error

type File

type File struct {
	afero.File
	Tags *string
}

File type to be used by Uploader. A wrapper around afero.File that allows attaching some additional metadata

type PrimeUploader

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

PrimeUploader encapsulates a few common processes: creating Uploads for a Document, generating pre-signed URLs for file access, and deleting Uploads.

func NewPrimeUploader

func NewPrimeUploader(storer storage.FileStorer, fileSizeLimit ByteSize) (*PrimeUploader, error)

NewPrimeUploader creates and returns a new uploader

func (*PrimeUploader) CreatePrimeUploadForDocument

func (u *PrimeUploader) CreatePrimeUploadForDocument(appCtx appcontext.AppContext, posID *uuid.UUID, contractorID uuid.UUID, file File, allowedTypes AllowedFileTypes) (*models.PrimeUpload, *validate.Errors, error)

CreatePrimeUploadForDocument creates a new PrimeUpload by performing validations, storing the specified file using the supplied storer, and saving an PrimeUpload object to the database containing the file's metadata.

func (*PrimeUploader) DeletePrimeUpload

func (u *PrimeUploader) DeletePrimeUpload(appCtx appcontext.AppContext, primeUpload *models.PrimeUpload) error

DeletePrimeUpload removes an PrimeUpload from the database and deletes its file from the storer.

func (*PrimeUploader) Download

func (u *PrimeUploader) Download(appCtx appcontext.AppContext, primeUpload *models.PrimeUpload) (io.ReadCloser, error)

Download fetches an Upload's file and stores it in a tempfile. The path to this file is returned.

It is the caller's responsibility to delete the tempfile.

func (*PrimeUploader) FileSystem

func (u *PrimeUploader) FileSystem() *afero.Afero

FileSystem return Uploader file system

func (*PrimeUploader) GetUploadStorageKey

func (u *PrimeUploader) GetUploadStorageKey() string

GetUploadStorageKey returns the PrimeUpload.Upload.StorageKey member

func (*PrimeUploader) PrepareFileForUpload

func (u *PrimeUploader) PrepareFileForUpload(appCtx appcontext.AppContext, file io.ReadCloser, filename string) (afero.File, error)

PrepareFileForUpload called Uploader.PrepareFileForUpload

func (*PrimeUploader) PresignedURL

func (u *PrimeUploader) PresignedURL(appCtx appcontext.AppContext, primeUpload *models.PrimeUpload) (string, error)

PresignedURL returns a URL that can be used to access an PrimeUpload's file.

func (*PrimeUploader) SetUploadStorageKey

func (u *PrimeUploader) SetUploadStorageKey(key string)

SetUploadStorageKey set the PrimeUpload.Upload.StorageKey member

func (*PrimeUploader) Uploader

func (u *PrimeUploader) Uploader() *Uploader

Uploader return Uploader

type ServiceRequestUploader

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

ServiceRequestUploader encapsulates a few common processes: creating Uploads for a Document, generating pre-signed URLs for file access, and deleting Uploads.

func NewServiceRequestUploader

func NewServiceRequestUploader(storer storage.FileStorer, fileSizeLimit ByteSize) (*ServiceRequestUploader, error)

NewServiceRequestUploader creates and returns a new uploader

func (*ServiceRequestUploader) CreateServiceRequestUploadForDocument

func (u *ServiceRequestUploader) CreateServiceRequestUploadForDocument(appCtx appcontext.AppContext, posID *uuid.UUID, contractorID uuid.UUID, file File, allowedTypes AllowedFileTypes) (*models.ServiceRequestDocumentUpload, *validate.Errors, error)

CreateServiceRequestUploadForDocument creates a new ServiceRequestUpload by performing validations, storing the specified file using the supplied storer, and saving an ServiceRequestUpload object to the database containing the file's metadata.

func (*ServiceRequestUploader) DeleteServiceRequestUpload

func (u *ServiceRequestUploader) DeleteServiceRequestUpload(appCtx appcontext.AppContext, serviceRequestUpload *models.ServiceRequestDocumentUpload) error

DeleteServiceRequestUpload removes an ServiceRequestUpload from the database and deletes its file from the storer.

func (*ServiceRequestUploader) Download

func (u *ServiceRequestUploader) Download(appCtx appcontext.AppContext, serviceRequestUpload *models.ServiceRequestDocumentUpload) (io.ReadCloser, error)

Download fetches an Upload's file and stores it in a tempfile. The path to this file is returned.

It is the caller's responsibility to delete the tempfile.

func (*ServiceRequestUploader) FileSystem

func (u *ServiceRequestUploader) FileSystem() *afero.Afero

FileSystem return Uploader file system

func (*ServiceRequestUploader) GetUploadStorageKey

func (u *ServiceRequestUploader) GetUploadStorageKey() string

GetUploadStorageKey returns the ServiceRequestUpload.Upload.StorageKey member

func (*ServiceRequestUploader) PrepareFileForUpload

func (u *ServiceRequestUploader) PrepareFileForUpload(appCtx appcontext.AppContext, file io.ReadCloser, filename string) (afero.File, error)

PrepareFileForUpload called Uploader.PrepareFileForUpload

func (*ServiceRequestUploader) PresignedURL

func (u *ServiceRequestUploader) PresignedURL(appCtx appcontext.AppContext, serviceRequestUpload *models.ServiceRequestDocumentUpload) (string, error)

PresignedURL returns a URL that can be used to access an ServiceRequestUpload's file.

func (*ServiceRequestUploader) SetUploadStorageKey

func (u *ServiceRequestUploader) SetUploadStorageKey(key string)

SetUploadStorageKey set the ServiceRequestUpload.Upload.StorageKey member

func (*ServiceRequestUploader) Uploader

func (u *ServiceRequestUploader) Uploader() *Uploader

Uploader return Uploader

type Uploader

type Uploader struct {
	Storer            storage.FileStorer
	UploadStorageKey  string
	DefaultStorageKey string
	FileSizeLimit     ByteSize
	UploadType        models.UploadType
}

Uploader encapsulates a few common processes: creating Uploads for a Document, generating pre-signed URLs for file access, and deleting Uploads.

func NewUploader

func NewUploader(storer storage.FileStorer, fileSizeLimit ByteSize, uploadType models.UploadType) (*Uploader, error)

NewUploader creates and returns a new uploader

func (*Uploader) CreateUpload

func (u *Uploader) CreateUpload(appCtx appcontext.AppContext, file File, allowedTypes AllowedFileTypes) (*models.Upload, *validate.Errors, error)

CreateUpload creates a new Upload by performing validations, storing the specified file using the supplied storer, and saving an Upload object to the database containing the file's metadata.

func (*Uploader) DeleteUpload

func (u *Uploader) DeleteUpload(appCtx appcontext.AppContext, upload *models.Upload) error

DeleteUpload removes an Upload from the database

func (*Uploader) Download

func (u *Uploader) Download(_ appcontext.AppContext, upload *models.Upload) (io.ReadCloser, error)

Download fetches an Upload's file and stores it in a tempfile. The path to this file is returned.

It is the caller's responsibility to delete the tempfile.

func (*Uploader) PrepareFileForUpload

func (u *Uploader) PrepareFileForUpload(appCtx appcontext.AppContext, file io.ReadCloser, filename string) (afero.File, error)

PrepareFileForUpload copy file buffer into Afero file, return Afero file

func (*Uploader) PresignedURL

func (u *Uploader) PresignedURL(appCtx appcontext.AppContext, upload *models.Upload) (string, error)

PresignedURL returns a URL that can be used to access an Upload's file.

func (*Uploader) SetUploadStorageKey

func (u *Uploader) SetUploadStorageKey(key string)

SetUploadStorageKey set the Upload.StorageKey member

type UserUploader

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

UserUploader encapsulates a few common processes: creating Uploads for a Document, generating pre-signed URLs for file access, and deleting Uploads.

func NewUserUploader

func NewUserUploader(storer storage.FileStorer, fileSizeLimit ByteSize) (*UserUploader, error)

NewUserUploader creates and returns a new uploader

func (*UserUploader) CreateUserUpload

func (u *UserUploader) CreateUserUpload(appCtx appcontext.AppContext, userID uuid.UUID, file File, allowedTypes AllowedFileTypes) (*models.UserUpload, *validate.Errors, error)

CreateUserUpload stores UserUpload but does not assign a Document

func (*UserUploader) CreateUserUploadForDocument

func (u *UserUploader) CreateUserUploadForDocument(appCtx appcontext.AppContext, documentID *uuid.UUID, userID uuid.UUID, file File, allowedTypes AllowedFileTypes) (*models.UserUpload, *validate.Errors, error)

CreateUserUploadForDocument creates a new UserUpload by performing validations, storing the specified file using the supplied storer, and saving an UserUpload object to the database containing the file's metadata.

func (*UserUploader) DeleteUserUpload

func (u *UserUploader) DeleteUserUpload(appCtx appcontext.AppContext, userUpload *models.UserUpload) error

DeleteUserUpload removes an UserUpload from the database and deletes its file from the storer.

func (*UserUploader) Download

func (u *UserUploader) Download(appCtx appcontext.AppContext, userUpload *models.UserUpload) (io.ReadCloser, error)

Download fetches an Upload's file and stores it in a tempfile. The path to this file is returned.

It is the caller's responsibility to delete the tempfile.

func (*UserUploader) FileSystem

func (u *UserUploader) FileSystem() *afero.Afero

FileSystem return file system from Uploader file storer

func (*UserUploader) GetUploadStorageKey

func (u *UserUploader) GetUploadStorageKey() string

GetUploadStorageKey returns the UserUpload.Upload.StorageKey member

func (*UserUploader) PrepareFileForUpload

func (u *UserUploader) PrepareFileForUpload(appCtx appcontext.AppContext, file io.ReadCloser, filename string) (afero.File, error)

PrepareFileForUpload calls Uploader.PrepareFileForUpload

func (*UserUploader) PresignedURL

func (u *UserUploader) PresignedURL(appCtx appcontext.AppContext, userUpload *models.UserUpload) (string, error)

PresignedURL returns a URL that can be used to access an UserUpload's file.

func (*UserUploader) SetUploadStorageKey

func (u *UserUploader) SetUploadStorageKey(key string)

SetUploadStorageKey set the UserUpload.Upload.StorageKey member

func (*UserUploader) Uploader

func (u *UserUploader) Uploader() *Uploader

Uploader return the Uploader for UserUploader

Jump to

Keyboard shortcuts

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