uploading files and a lot more

This commit is contained in:
Zoe
2024-09-08 07:15:43 -05:00
parent 13218116e3
commit 86ef860568
28 changed files with 1447 additions and 107 deletions

View File

@@ -3,8 +3,11 @@ package routes
import (
"context"
"filething/models"
"fmt"
"net/http"
"os"
"regexp"
"time"
"github.com/google/uuid"
"github.com/labstack/echo/v4"
@@ -40,33 +43,17 @@ func LoginHandler(c echo.Context) error {
return c.JSON(http.StatusInternalServerError, map[string]string{"message": "An unknown error occoured!"})
}
expiration := time.Now().Add(time.Hour * 24 * 365 * 100)
c.SetCookie(&http.Cookie{
Name: "sessionToken",
Value: session.ID.String(),
SameSite: http.SameSiteStrictMode,
Expires: expiration,
Path: "/",
})
return c.JSON(http.StatusOK, map[string]string{"message": "Login successful!"})
// sessionID := uuid.New().String()
// session := &models.Session{ID: sessionID, UserID: user.ID, ExpiresAt: time.Now().Add(time.Hour * 24)}
// key := "session:" + session.ID
// err = client.HSet(ctx, key, session).Err()
// if err != nil {
// c.JSON(http.StatusInternalServerError, gin.H{"message": "An unknown error occoured!"})
// return
// }
// http.SetCookie(c.Writer, &http.Cookie{
// Name: "sessionToken",
// Value: sessionID,
// Path: "/",
// })
// c.JSON(http.StatusOK, gin.H{"message": "Login successful"})
}
func SignupHandler(c echo.Context) error {
@@ -96,6 +83,7 @@ func SignupHandler(c echo.Context) error {
Username: signupData.Username,
Email: signupData.Email,
PasswordHash: string(hash),
PlanID: 1, // basic 10GB plan
}
_, err = db.NewInsert().Model(user).Exec(context.Background())
@@ -103,28 +91,33 @@ func SignupHandler(c echo.Context) error {
return c.JSON(http.StatusConflict, map[string]string{"message": "A user with that email or username already exists!"})
}
err = os.Mkdir(fmt.Sprintf("%s/%s", os.Getenv("STORAGE_PATH"), user.ID), os.ModePerm)
if err != nil {
fmt.Println(err)
return err
}
if err != nil {
return c.JSON(http.StatusInternalServerError, map[string]string{"message": "An unknown error occoured!"})
}
session, err := GenerateSessionToken(db, user.ID)
if err != nil {
return c.JSON(http.StatusInternalServerError, map[string]string{"message": "An unknown error occoured!"})
}
expiration := time.Now().Add(time.Hour * 24 * 365 * 100)
c.SetCookie(&http.Cookie{
Name: "sessionToken",
Value: session.ID.String(),
SameSite: http.SameSiteStrictMode,
Expires: expiration,
Path: "/",
})
return c.JSON(http.StatusOK, map[string]string{"message": "Signup successful!"})
// http.SetCookie(c.Writer, &http.Cookie{
// Name: "sessionToken",
// Value: sessionID,
// Path: "/",
// })
// c.JSON(http.StatusOK, gin.H{"message": "Signup successful"})
}
func GenerateSessionToken(db *bun.DB, userId uuid.UUID) (*models.Session, error) {
@@ -136,3 +129,12 @@ func GenerateSessionToken(db *bun.DB, userId uuid.UUID) (*models.Session, error)
return session, err
}
func GetUser(c echo.Context) error {
user := c.Get("user")
if user == nil {
return c.JSON(http.StatusNotFound, map[string]string{"message": "User not found"})
}
return c.JSON(http.StatusOK, user.(*models.User))
}

194
routes/files.go Normal file
View File

@@ -0,0 +1,194 @@
package routes
import (
"filething/models"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/labstack/echo/v4"
)
type UploadResponse struct {
Usage int64 `json:"usage"`
File File `json:"file"`
}
func UploadFile(c echo.Context) error {
user := c.Get("user").(*models.User)
fullPath := strings.Trim(c.Param("*"), "/")
basePath := fmt.Sprintf("%s/%s/%s/", os.Getenv("STORAGE_PATH"), user.ID, fullPath)
currentUsage, err := calculateStorageUsage(basePath)
if err != nil {
fmt.Println(err)
return err
}
reader, err := c.Request().MultipartReader()
if err != nil {
fmt.Println(err)
return err
}
part, err := reader.NextPart()
if err != nil {
fmt.Println(err)
return err
}
filepath := filepath.Join(basePath, part.FileName())
if _, err = os.Stat(filepath); err == nil {
return c.JSON(http.StatusConflict, map[string]string{"message": "File with that name already exists"})
}
dst, err := os.Create(filepath)
if err != nil {
fmt.Println(err)
return err
}
defer dst.Close()
// Read the file manually because otherwise we are limited by the arbitrarily small size of /tmp
buffer := make([]byte, 4096)
totalSize := int64(0)
for {
n, readErr := part.Read(buffer)
if readErr != nil && readErr == io.ErrUnexpectedEOF {
dst.Close()
os.Remove(filepath)
return c.JSON(http.StatusRequestTimeout, map[string]string{"message": "Upload canceled"})
}
if readErr != nil && readErr != io.EOF {
fmt.Println(err)
return readErr
}
totalSize += int64(n)
if currentUsage+totalSize > user.Plan.MaxStorage {
dst.Close()
os.Remove(filepath)
return c.JSON(http.StatusInsufficientStorage, map[string]string{"message": "Insufficient storage space"})
}
if _, err := dst.Write(buffer[:n]); err != nil {
fmt.Println(err)
return err
}
if n == 0 || readErr == io.EOF {
entry, err := os.Stat(filepath)
if err != nil {
fmt.Println(err)
return err
}
uploadFile := &UploadResponse{
Usage: currentUsage + totalSize,
File: File{
Name: entry.Name(),
IsDir: entry.IsDir(),
Size: entry.Size(),
LastModified: entry.ModTime().Format("1/2/2006"),
},
}
return c.JSON(http.StatusOK, uploadFile)
}
}
}
func calculateStorageUsage(basePath string) (int64, error) {
var totalSize int64
// Read the directory
entries, err := os.ReadDir(basePath)
if err != nil {
return 0, err
}
// Iterate over directory entries
for _, entry := range entries {
if entry.IsDir() {
// Recursively calculate size of directories
dirPath := filepath.Join(basePath, entry.Name())
dirSize, err := calculateStorageUsage(dirPath)
if err != nil {
return 0, err
}
totalSize += dirSize
} else {
// Calculate size of file
_ = filepath.Join(basePath, entry.Name())
fileInfo, err := entry.Info()
if err != nil {
return 0, err
}
totalSize += fileInfo.Size()
}
}
return totalSize, nil
}
type File struct {
Name string `json:"name"`
IsDir bool `json:"is_dir"`
Size int64 `json:"size"`
LastModified string `json:"last_modified"`
}
func GetFiles(c echo.Context) error {
user := c.Get("user").(*models.User)
fullPath := strings.Trim(c.Param("*"), "/")
basePath := fmt.Sprintf("%s/%s/%s/", os.Getenv("STORAGE_PATH"), user.ID, fullPath)
f, err := os.Open(basePath)
if err != nil {
fmt.Println(err)
return err
}
files, err := f.Readdir(0)
if err != nil {
fmt.Println(err)
return err
}
jsonFiles := make([]File, 0)
for _, f := range files {
jsonFiles = append(jsonFiles, File{
Name: f.Name(),
IsDir: f.IsDir(),
Size: f.Size(),
LastModified: f.ModTime().Format("1/2/2006"),
})
}
return c.JSON(http.StatusOK, jsonFiles)
}
func GetUsage(c echo.Context) error {
user := c.Get("user").(*models.User)
fullPath := strings.Trim(c.Param("*"), "/")
basePath := fmt.Sprintf("%s/%s/%s/", os.Getenv("STORAGE_PATH"), user.ID, fullPath)
storageUsage, err := calculateStorageUsage(basePath)
if err != nil {
return err
}
return c.JSON(http.StatusOK, map[string]int64{"usage": storageUsage})
}