delete token & upload response struct to return delete token and sha256 as hex

This commit is contained in:
2026-11-10 12:05:33 -05:00
parent 33b1b579d6
commit 4c25a66f0f
3 changed files with 15 additions and 5 deletions
+1
View File
@@ -25,6 +25,7 @@ type Upload struct {
CreatedAt int64 `gorm:"column:created_at;not null;autoCreateTime" json:"created_at"` CreatedAt int64 `gorm:"column:created_at;not null;autoCreateTime" json:"created_at"`
ExpiresAt *int64 `gorm:"column:expires_at;index" json:"expires_at"` // null means no expiration ExpiresAt *int64 `gorm:"column:expires_at;index" json:"expires_at"` // null means no expiration
LastAccessedAt int64 `gorm:"column:last_accessed_at;not null;autoCreateTime;index" json:"last_accessed_at"` LastAccessedAt int64 `gorm:"column:last_accessed_at;not null;autoCreateTime;index" json:"last_accessed_at"`
DeleteToken string `gorm:"column:delete_token;not null" json:"-"`
} }
func initDatabase(path string) (*gorm.DB, error) { func initDatabase(path string) (*gorm.DB, error) {
+12 -3
View File
@@ -34,6 +34,11 @@ type uploadInput struct {
MIMEType string MIMEType string
Type ContentType Type ContentType
} }
type UploadResponse struct {
Upload
SHA256 string `json:"sha256"`
DeleteToken string `json:"deleteToken"`
}
func uploadHandler(db *gorm.DB, cfg Config) http.HandlerFunc { func uploadHandler(db *gorm.DB, cfg Config) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
@@ -56,7 +61,7 @@ func uploadHandler(db *gorm.DB, cfg Config) http.HandlerFunc {
idLength = 10 idLength = 10
} }
var input uploadInput var input UploadInput
switch mediaType { switch mediaType {
case "multipart/form-data": // file uploads case "multipart/form-data": // file uploads
r.Body = http.MaxBytesReader(w, r.Body, cfg.MaxFileBytes) r.Body = http.MaxBytesReader(w, r.Body, cfg.MaxFileBytes)
@@ -115,7 +120,7 @@ func uploadHandler(db *gorm.DB, cfg Config) http.HandlerFunc {
return return
} }
input = uploadInput{ input = UploadInput{
Reader: strings.NewReader(content), Reader: strings.NewReader(content),
Filename: "paste.txt", Filename: "paste.txt",
MIMEType: "text/plain; charset=utf-8", MIMEType: "text/plain; charset=utf-8",
@@ -196,7 +201,11 @@ func uploadHandler(db *gorm.DB, cfg Config) http.HandlerFunc {
saved = true saved = true
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated) w.WriteHeader(http.StatusCreated)
if err := json.NewEncoder(w).Encode(upload); err != nil { if err := json.NewEncoder(w).Encode(UploadResponse{
Upload: upload,
SHA256: hex.EncodeToString(upload.SHA256),
DeleteToken: upload.DeleteToken,
}); err != nil {
log.Println("failed to encode response:", err) log.Println("failed to encode response:", err)
} }
} }
+2 -2
View File
@@ -8,13 +8,13 @@
<body> <body>
<h1>{{.Title}}</h1> <h1>{{.Title}}</h1>
<p>{{.Description}}</p> <p>{{.Description}}</p>
<form action="/paste" method="post"> <form action="/upload" method="post">
<label for="content">paste content</label> <label for="content">paste content</label>
<textarea id="content" name="content" rows="10" required></textarea> <textarea id="content" name="content" rows="10" required></textarea>
<button type="submit">upload paste</button> <button type="submit">upload paste</button>
</form> </form>
<form action="/file" method="post" enctype="multipart/form-data"> <form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" required name="file"/> <input type="file" required name="file"/>
<button type="submit">upload File</button> <button type="submit">upload File</button>
</form> </form>