common

package module
v0.0.0-...-a1b3fe6 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2023 License: GPL-3.0 Imports: 6 Imported by: 2

Documentation

Overview

Copyright © 2023 Thomas von Dein

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.

Index

Constants

View Source
const (
	TypeUpload = iota
	TypeForm
)

Variables

This section is empty.

Functions

func Duration2int

func Duration2int(duration string) int

We could use time.ParseDuration(), but this doesn't support days.

We could also use github.com/xhit/go-str2duration/v2, which does the job, but it's just another dependency, just for this little gem. And we don't need a time.Time value.

Convert a duration into seconds (int). Valid time units are "s", "m", "h" and "d".

func GetContext

func GetContext(j []byte) (string, error)

Extract context, whatever kind entry is, but we don't know in advance, only after unmarshalling. So try Upload first, if that fails, try Form.

func Untaint

func Untaint(input string, wanted *regexp.Regexp) (string, error)

Untaint user input, that is: remove all non supported chars.

wanted is a regexp matching chars we shall leave. Everything else will be removed. Eg:

untainted := Untaint(input, `[^a-zA-Z0-9\-]`)

Returns a new string and an error if the input string has been modified. It's the callers choice to decide what to do about it. You may ignore the error and use the untainted string or bail out.

Types

type Dbentry

type Dbentry interface {
	Getcontext(j []byte) (string, error)
	Marshal() ([]byte, error)
	MatchExpire(r *regexp.Regexp) bool
	MatchDescription(r *regexp.Regexp) bool
	MatchFile(r *regexp.Regexp) bool
	MatchCreated(r *regexp.Regexp) bool
	IsType(t int) bool
}

upload or form structs

func Unmarshal

func Unmarshal(j []byte, t int) (Dbentry, error)

type Form

type Form struct {
	// Note the dual use of the Id: it will be used as onetime api key
	// from generated upload forms and  stored in the session store so
	// that the upload handler is able to check if the form object has
	// to be deleted immediately (if  its expire field has been set to
	// asap)
	Type        int       `json:"type"`
	Id          string    `json:"id"`
	Expire      string    `json:"expire"`
	Description string    `json:"description"`
	Created     Timestamp `json:"uploaded"`
	Context     string    `json:"context"`
	Url         string    `json:"url"`
	Notify      string    `json:"notify"`
}

func (Form) Getcontext

func (form Form) Getcontext(j []byte) (string, error)

func (Form) IsType

func (form Form) IsType(t int) bool

func (Form) Marshal

func (form Form) Marshal() ([]byte, error)

func (Form) MatchCreated

func (form Form) MatchCreated(r *regexp.Regexp) bool

func (Form) MatchDescription

func (form Form) MatchDescription(r *regexp.Regexp) bool

func (Form) MatchExpire

func (form Form) MatchExpire(r *regexp.Regexp) bool

func (Form) MatchFile

func (form Form) MatchFile(r *regexp.Regexp) bool

type Response

type Response struct {
	Uploads []*Upload `json:"uploads"`
	Forms   []*Form   `json:"forms"`

	// integrate the Result struct so we can signal success
	Result
}

this one is also used for marshalling to the client

func (*Response) Append

func (r *Response) Append(entry Dbentry)

Response methods

type Result

type Result struct {
	Success bool   `json:"success"`
	Message string `json:"message"`
	Code    int    `json:"code"`
}

used to return to the api client

type Timestamp

type Timestamp struct {
	time.Time
	// contains filtered or unexported fields
}

Timestamp is like time.Time, but knows how to unmarshal from JSON Unix timestamp numbers or RFC3339 strings, and marshal back into the same JSON representation.

func (Timestamp) MarshalJSON

func (t Timestamp) MarshalJSON() ([]byte, error)

func (*Timestamp) UnmarshalJSON

func (t *Timestamp) UnmarshalJSON(data []byte) error

type Upload

type Upload struct {
	Type        int       `json:"type"`
	Id          string    `json:"id"`
	Expire      string    `json:"expire"`
	File        string    `json:"file"`    // final filename (visible to the downloader)
	Members     []string  `json:"members"` // contains multiple files, so File is an archive
	Created     Timestamp `json:"uploaded"`
	Context     string    `json:"context"`
	Description string    `json:"description"`
	Url         string    `json:"url"`
}

func (Upload) Getcontext

func (upload Upload) Getcontext(j []byte) (string, error)

implement Dbentry interface

func (Upload) IsType

func (upload Upload) IsType(t int) bool

func (Upload) Marshal

func (upload Upload) Marshal() ([]byte, error)

func (Upload) MatchCreated

func (upload Upload) MatchCreated(r *regexp.Regexp) bool

func (Upload) MatchDescription

func (upload Upload) MatchDescription(r *regexp.Regexp) bool

func (Upload) MatchExpire

func (upload Upload) MatchExpire(r *regexp.Regexp) bool

func (Upload) MatchFile

func (upload Upload) MatchFile(r *regexp.Regexp) bool

Jump to

Keyboard shortcuts

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