mirror of
https://codeberg.org/scip/ephemerup.git
synced 2025-12-16 20:20:58 +01:00
simplified appends
This commit is contained in:
22
api/db.go
22
api/db.go
@@ -130,21 +130,13 @@ func (db *Db) UploadsList(apicontext string, filter string, t int) (*common.Resp
|
||||
if apicontext == entryContext {
|
||||
// unless a filter needed OR no filter specified
|
||||
if (filter != "" && entryContext == filter) || filter == "" {
|
||||
if t == common.TypeUpload {
|
||||
response.Uploads = append(response.Uploads, entry.(*common.Upload))
|
||||
} else {
|
||||
response.Forms = append(response.Forms, entry.(*common.Form))
|
||||
}
|
||||
response.Append(entry)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// return all, because we operate a public service or current==super
|
||||
if (filter != "" && entryContext == filter) || filter == "" {
|
||||
if t == common.TypeUpload {
|
||||
response.Uploads = append(response.Uploads, entry.(*common.Upload))
|
||||
} else {
|
||||
response.Forms = append(response.Forms, entry.(*common.Form))
|
||||
}
|
||||
response.Append(entry)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -179,19 +171,15 @@ func (db *Db) Get(apicontext string, id string, t int) (*common.Response, error)
|
||||
|
||||
var entryContext string
|
||||
if t == common.TypeUpload {
|
||||
entryContext = entry.(common.Upload).Context
|
||||
entryContext = entry.(*common.Upload).Context
|
||||
} else {
|
||||
entryContext = entry.(common.Form).Context
|
||||
entryContext = entry.(*common.Form).Context
|
||||
}
|
||||
|
||||
if (apicontext != "" && (db.cfg.Super == apicontext || entryContext == apicontext)) || apicontext == "" {
|
||||
// allowed if no context (public or download)
|
||||
// or if context matches or if context==super
|
||||
if t == common.TypeUpload {
|
||||
response.Uploads = append(response.Uploads, entry.(*common.Upload))
|
||||
} else {
|
||||
response.Forms = append(response.Forms, entry.(*common.Form))
|
||||
}
|
||||
response.Append(entry)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
@@ -105,6 +105,24 @@ func (form Form) Marshal() ([]byte, error) {
|
||||
return jsonentry, nil
|
||||
}
|
||||
|
||||
/*
|
||||
Response methods
|
||||
*/
|
||||
func (r *Response) Append(entry Dbentry) {
|
||||
switch entry.(type) {
|
||||
case *Upload:
|
||||
r.Uploads = append(r.Uploads, entry.(*Upload))
|
||||
case Upload:
|
||||
r.Uploads = append(r.Uploads, entry.(*Upload))
|
||||
case Form:
|
||||
r.Forms = append(r.Forms, entry.(*Form))
|
||||
case *Form:
|
||||
r.Forms = append(r.Forms, entry.(*Form))
|
||||
default:
|
||||
panic("unknown type!")
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Extract context, whatever kind entry is, but we don't know in
|
||||
advance, only after unmarshalling. So try Upload first, if that
|
||||
|
||||
@@ -219,7 +219,7 @@ func List(w io.Writer, c *cfg.Config, args []string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
return RespondTable(w, resp)
|
||||
return UploadsRespondTable(w, resp)
|
||||
}
|
||||
|
||||
func Delete(w io.Writer, c *cfg.Config, args []string) error {
|
||||
@@ -249,7 +249,7 @@ func Describe(w io.Writer, c *cfg.Config, args []string) error {
|
||||
|
||||
id := args[0] // we describe only 1 object
|
||||
|
||||
rq := Setup(c, "/uploads/"+id+"/")
|
||||
rq := Setup(c, "/uploads/"+id)
|
||||
resp, err := rq.R.Get(rq.Url)
|
||||
|
||||
if err != nil {
|
||||
|
||||
@@ -66,12 +66,17 @@ func WriteTable(w io.Writer, headers []string, data [][]string) {
|
||||
fmt.Fprintln(w, tableString.String())
|
||||
}
|
||||
|
||||
// output like psql \x
|
||||
func WriteExtended(w io.Writer, uploads *common.Uploads) {
|
||||
/* Print output like psql \x
|
||||
|
||||
Prints all Uploads and Forms which exist in common.Response,
|
||||
however, we expect only one kind of them to be actually filled, so
|
||||
the function can be used for forms and uploads.
|
||||
*/
|
||||
func WriteExtended(w io.Writer, response *common.Response) {
|
||||
format := fmt.Sprintf("%%%ds: %%s\n", Maxwidth)
|
||||
|
||||
// we shall only have 1 element, however, if we ever support more, here we go
|
||||
for _, entry := range uploads.Entries {
|
||||
for _, entry := range response.Uploads {
|
||||
expire := prepareExpire(entry.Expire, entry.Uploaded)
|
||||
fmt.Fprintf(w, format, "Id", entry.Id)
|
||||
fmt.Fprintf(w, format, "Expire", expire)
|
||||
@@ -81,37 +86,39 @@ func WriteExtended(w io.Writer, uploads *common.Uploads) {
|
||||
fmt.Fprintf(w, format, "Url", entry.Url)
|
||||
fmt.Fprintln(w)
|
||||
}
|
||||
|
||||
// FIXME: add response.Forms loop here
|
||||
}
|
||||
|
||||
// extract an common.Uploads{} struct from json response
|
||||
func GetUploadsFromResponse(resp *req.Response) (*common.Uploads, error) {
|
||||
uploads := common.Uploads{}
|
||||
func GetResponse(resp *req.Response) (*common.Response, error) {
|
||||
response := common.Response{}
|
||||
|
||||
if err := json.Unmarshal([]byte(resp.String()), &uploads); err != nil {
|
||||
if err := json.Unmarshal([]byte(resp.String()), &response); err != nil {
|
||||
return nil, errors.New("Could not unmarshall JSON response: " + err.Error())
|
||||
}
|
||||
|
||||
if !uploads.Success {
|
||||
return nil, errors.New(uploads.Message)
|
||||
if !response.Success {
|
||||
return nil, errors.New(response.Message)
|
||||
}
|
||||
|
||||
return &uploads, nil
|
||||
return &response, nil
|
||||
}
|
||||
|
||||
// turn the Uploads{} struct into a table and print it
|
||||
func RespondTable(w io.Writer, resp *req.Response) error {
|
||||
uploads, err := GetUploadsFromResponse(resp)
|
||||
func UploadsRespondTable(w io.Writer, resp *req.Response) error {
|
||||
response, err := GetResponse(resp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if uploads.Message != "" {
|
||||
fmt.Fprintln(w, uploads.Message)
|
||||
if response.Message != "" {
|
||||
fmt.Fprintln(w, response.Message)
|
||||
}
|
||||
|
||||
// tablewriter
|
||||
data := [][]string{}
|
||||
for _, entry := range uploads.Entries {
|
||||
for _, entry := range response.Uploads {
|
||||
data = append(data, []string{
|
||||
entry.Id, entry.Expire, entry.Context, entry.Uploaded.Format("2006-01-02 15:04:05"),
|
||||
})
|
||||
@@ -124,16 +131,16 @@ func RespondTable(w io.Writer, resp *req.Response) error {
|
||||
|
||||
// turn the Uploads{} struct into xtnd output and print it
|
||||
func RespondExtended(w io.Writer, resp *req.Response) error {
|
||||
uploads, err := GetUploadsFromResponse(resp)
|
||||
response, err := GetResponse(resp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if uploads.Message != "" {
|
||||
fmt.Fprintln(w, uploads.Message)
|
||||
if response.Message != "" {
|
||||
fmt.Fprintln(w, response.Message)
|
||||
}
|
||||
|
||||
WriteExtended(w, uploads)
|
||||
WriteExtended(w, response)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user