Commit ccdbd497 authored by Lukas Böhm's avatar Lukas Böhm 🎱
Browse files

some api changes

parent bd748300
Pipeline #59177 failed with stages
in 10 seconds
......@@ -3,6 +3,7 @@ package main
import (
"encoding/json"
"fmt"
"github.com/google/uuid"
"github.com/gorilla/mux"
"github.com/rs/cors"
"gorm.io/driver/sqlserver"
......@@ -24,7 +25,10 @@ const CHUNK_SIZE = (10 << 20)
func AllShares(w http.ResponseWriter, r *http.Request) {
db := GetDatabase()
var shares []Share
db.Where("is_public = ?", 1).Find(&shares)
err := db.Where("is_public = ? AND is_temporary = false", 1).Find(&shares).Error
if err != nil {
fmt.Println(err)
}
SendJSON(w, shares)
}
......@@ -32,27 +36,37 @@ func AllShares(w http.ResponseWriter, r *http.Request) {
func GetShare(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
db := GetDatabase()
var share Share
db.Where("ID = ?", vars["id"]).Find(&share)
err := db.Where("ID = ?", vars["id"]).First(&share).Error
if err != nil {
w.WriteHeader(http.StatusNotFound)
return
} else if share.IsTemporary == true {
w.WriteHeader(http.StatusFailedDependency)
return
} else if share.Password != "" {
w.WriteHeader(http.StatusForbidden)
return
}
SendJSON(w, share)
}
// PUT: /shares
func OpenShare(w http.ResponseWriter, r *http.Request) {
log.Println("OpenShare")
db := GetDatabase()
// parse body
reqBody, err := ioutil.ReadAll(r.Body)
if err != nil {
fmt.Println("parsing body kaputt")
log.Fatal(err)
}
// read in json
// parse in json
var newShare Share
json.Unmarshal(reqBody, &newShare)
// create temporary db entries
db.Create(&newShare)
......@@ -63,28 +77,33 @@ func OpenShare(w http.ResponseWriter, r *http.Request) {
}
SendJSON(w, newShare)
return
}
// POST /share/:id
func CloseShare(w http.ResponseWriter, r *http.Request) {
log.Println("CloseShare")
vars := mux.Vars(r)
db := GetDatabase()
// get stuff
var share Share
err := db.Where("id = ?", vars["id"]).First(&share).Error
if err != nil {
fmt.Println(err)
return
} else if share.IsTemporary == false {
return
}
// move files to permanent location
//oldPath := filepath.Join(MEDIA_DIR, "temp", vars["id"])
//newPath := filepath.Join(MEDIA_DIR, "data", vars["id"])
//err := os.Rename(oldPath, newPath)
//if err != nil {
// log.Fatal(err)
//}
oldPath := filepath.Join(MEDIA_DIR, "temp", vars["id"])
newPath := filepath.Join(MEDIA_DIR, "data", vars["id"])
err = os.Rename(oldPath, newPath)
if err != nil {
fmt.Println(err)
}
// set stuff permanent
var share Share
db.Where("id = ?", vars["id"]).Find(&share)
share.IsLocked = true
share.IsTemporary = false
db.Save(&share)
......@@ -96,8 +115,9 @@ func UploadAttachment(w http.ResponseWriter, r *http.Request) {
log.Println("UploadTest")
vars := mux.Vars(r)
db := GetDatabase()
fmt.Fprintf(w, "Uploading File")
// Parse file from body
r.ParseMultipartForm(CHUNK_SIZE) // Maximum 10 MB in RAM
file, handler, err := r.FormFile("files")
if err != nil {
......@@ -106,6 +126,7 @@ func UploadAttachment(w http.ResponseWriter, r *http.Request) {
return
}
defer file.Close()
// save file
fileBytes, err := ioutil.ReadAll(file)
if err != nil {
......@@ -115,7 +136,19 @@ func UploadAttachment(w http.ResponseWriter, r *http.Request) {
if err != nil {
fmt.Println(err)
}
fmt.Fprintf(w, "Successfully Uploaded File\n")
// add db entry
var att Attachment
sid, err := uuid.Parse(vars["id"])
if err != nil {
log.Fatal(err)
}
att.ShareID = sid
att.Filename = handler.Filename
att.Filesize = handler.Size
db.Create(&att)
SendJSON(w, att)
}
......
......@@ -11,12 +11,12 @@ import (
type Share struct {
ID uuid.UUID `json:"id" gorm:"unique; not null"`
Name string `json:"name"`
Expires time.Time `json:"expires"`
DownloadLimit int `json:"downloadLimit"`
Expires time.Time `json:"expires,omitempty"`
DownloadLimit int `json:"downloadLimit,omitempty"`
IsPublic bool `json:"is_public" gorm:"not null; default:false; index"`
Password string `json:"password"`
IsTemporary bool `gorm:"not null; default:true"`
IsLocked bool `gorm:"not null; default:false"`
Password string `json:"-"`
Email string `json:"email"`
IsTemporary bool `json:"-" gorm:"not null; default:true"`
Attachments []Attachment `json:"files" gorm:"foreignKey:ShareID; constraint:OnDelete:CASCADE"`
}
......@@ -25,7 +25,6 @@ type Attachment struct {
ID uuid.UUID `json:"id" gorm:"unique; not null"`
Filename string `json:"filename" gorm:"not null"`
Filesize int64 `json:"filesize" gorm:"not null; default:0"`
IsSaved bool `json:"is_saved" gorm:"not null; default:0"`
ShareID uuid.UUID `json:"share_id" gorm:"not null"`
}
......
......@@ -5,6 +5,7 @@ info:
servers:
- url: https://send.chief.zone/api
paths:
/shares:
get:
summary: list all public shares
......@@ -32,6 +33,10 @@ paths:
default:
description: unexpected error
/share/{id}:
get:
summary: get one share
......@@ -70,8 +75,7 @@ paths:
$ref: "#/components/schemas/Share"
default:
description: unexpected error
/share/{id}/attachment/{att}:
put:
summary: upload one attachment
operationId: UploadAttachment
......@@ -81,11 +85,6 @@ paths:
required: true
schema:
type: string
- name: att
in: path
required: true
schema:
type: string
responses:
'201': # vllt auch 204
description: empty
......@@ -99,17 +98,15 @@ paths:
required: true
schema:
type: string
- name: att
in: path
required: true
schema:
type: string
responses:
'200':
description: empty
components:
schemas:
Share:
type: object
properties:
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment