helper

package
v0.0.0-...-0985497 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2024 License: GPL-3.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FileDelete

func FileDelete(path string) error

FileDelete deletes an file

func FileExists

func FileExists(path string) bool

FileExists checks if a file really exists.

func FileTouch

func FileTouch(path string) (err error)

FileTouch creates an empty file

func Int64FromURL

func Int64FromURL(r *http.Request, name string, standard int64) int64

Int64FromURL will read an URL parameter like /api/?some_int=3

func IntFromURL

func IntFromURL(r *http.Request, name string, standard int) int

IntFromURL will read an URL parameter like /api/?some_int=3

func IsJpegFile

func IsJpegFile(buf []byte) bool

IsJpegFile checks if file is jpg file based on magic number

func IsPdfFile

func IsPdfFile(buf []byte) bool

IsPdfFile checks if file is pdf file based on magic number

func IsPngFile

func IsPngFile(buf []byte) bool

IsPngFile checks if file is png file based on magic number

func IsZipFile

func IsZipFile(buf []byte) bool

IsZipFile checks if file is zip file based on magic number

func StringArrayFromURL

func StringArrayFromURL(r *http.Request, name string, standard []string) []string

StringArrayFromURL will read an URL parameter like /api/?some_strings=foo,bar

func StringArrayToIntArray

func StringArrayToIntArray(values []string) ([]int, error)

StringArrayToIntArray converts a list of strings into a list of int or failes

func StringFromURL

func StringFromURL(r *http.Request, name string, standard string) string

StringFromURL will read an URL parameter like /api/?some_string=foo

func Time

func Time(t time.Time) time.Time

Time returns time.Now() but without nanseconds for passing unit-tests. There are some issues with storing and retriebing the nanoseconds.

func ToH

func ToH(z interface{}) map[string]interface{}

ToH converts any object into an typeless object (used by unit tests).

Types

type DummyWriter

type DummyWriter struct{}

DummyWriter is a writer which does nothing (use when writing to disk)

func (DummyWriter) Header

func (h DummyWriter) Header() http.Header

Header returns empty header

func (DummyWriter) Write

func (h DummyWriter) Write([]byte) (int, error)

Write does nothing

func (DummyWriter) WriteHeader

func (h DummyWriter) WriteHeader(statusCode int)

WriteHeader does nothing

type FileCategory

type FileCategory int32

FileCategory represents the categorie any upload is associated with

const (
	AvatarCategory                FileCategory = 0
	SheetCategory                 FileCategory = 1
	PublicTestCategory            FileCategory = 2
	PrivateTestCategory           FileCategory = 3
	MaterialCategory              FileCategory = 4
	SubmissionCategory            FileCategory = 5
	SubmissionsCollectionCategory FileCategory = 6
)

all categories

type FileHandle

type FileHandle struct {
	Category   FileCategory
	ID         int64            // an unique identifier (e.g. from database)
	Extensions []string         //
	MaxBytes   bytefmt.ByteSize // 0 means no limit
	Infos      []int64
}

FileHandle represents all information for file being uploaded or downloaded.

func NewAvatarFileHandle

func NewAvatarFileHandle(userID int64) *FileHandle

NewAvatarFileHandle will handle user avatars. We support jpg only.

func NewMaterialFileHandle

func NewMaterialFileHandle(ID int64) *FileHandle

NewMaterialFileHandle will handle course slides or extra material (zip files).

func NewPrivateTestFileHandle

func NewPrivateTestFileHandle(ID int64) *FileHandle

NewPrivateTestFileHandle will handle the testing framework for private unit tests (zip files).

func NewPublicTestFileHandle

func NewPublicTestFileHandle(ID int64) *FileHandle

NewPublicTestFileHandle will handle the testing framework for public unit tests (zip files).

func NewSheetFileHandle

func NewSheetFileHandle(ID int64) *FileHandle

NewSheetFileHandle will handle exercise sheets (zip files).

func NewSubmissionFileHandle

func NewSubmissionFileHandle(ID int64) *FileHandle

NewSubmissionFileHandle will handle homework/exercise submissions (zip files).

func NewSubmissionsCollectionFileHandle

func NewSubmissionsCollectionFileHandle(courseID int64, sheetID int64,
	taskID int64, groupID int64) *FileHandle

NewSubmissionsCollectionFileHandle will handle a collection of submissions.

func (*FileHandle) Delete

func (f *FileHandle) Delete() error

Delete deletes a file from disk.

func (*FileHandle) Exists

func (f *FileHandle) Exists() bool

Exists checks if a file really exists.

func (*FileHandle) GetContentType

func (f *FileHandle) GetContentType() (string, error)

GetContentType tries to predict the content type without reading the entire file. There are some issues with this function as it cannot distinguish between zip and octstream.

func (*FileHandle) Path

func (f *FileHandle) Path() string

Path return the path to a file using the config

func (*FileHandle) Sha256

func (f *FileHandle) Sha256() (string, error)

Sha256 computes the checksum and return it as a string

func (*FileHandle) WriteToBody

func (f *FileHandle) WriteToBody(w http.ResponseWriter) error

WriteToBody will write a file from disk to the http response (download process)

func (*FileHandle) WriteToBodyWithName

func (f *FileHandle) WriteToBodyWithName(publicFilename string, w http.ResponseWriter) error

WriteToBodyWithName reads a file from disk a writes it in the HTTP response (download)

func (*FileHandle) WriteToDisk

func (f *FileHandle) WriteToDisk(r *http.Request, fieldName string) (string, error)

WriteToDisk will save uploads from a http request to the directory specified in the config.

type FileManager

type FileManager interface {
	WriteToBody(w http.ResponseWriter) error
	WriteToDisk(req multipart.File) error
	GetContentType() (string, error)
	Path(fallback bool) bool
	Delete() error
	Exists() bool
}

FileManager contains all operations we need to handle files within HTTP

type H

type H map[string]interface{}

H is a neat alias

Jump to

Keyboard shortcuts

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