feat: create new file page with improved UI, combine upload and download functionality
This commit is contained in:
135
handler/file/download/download.go
Normal file
135
handler/file/download/download.go
Normal file
@ -0,0 +1,135 @@
|
||||
package downloadHandler
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/fossyy/filekeeper/app"
|
||||
"github.com/fossyy/filekeeper/types/models"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func GET(w http.ResponseWriter, r *http.Request) {
|
||||
fileID := r.PathValue("id")
|
||||
file, err := app.Server.Database.GetFile(fileID)
|
||||
if err != nil {
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
app.Server.Logger.Error(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
uploadDir := "uploads"
|
||||
currentDir, _ := os.Getwd()
|
||||
basePath := filepath.Join(currentDir, uploadDir)
|
||||
saveFolder := filepath.Join(basePath, file.OwnerID.String(), file.ID.String())
|
||||
|
||||
if filepath.Dir(saveFolder) != filepath.Join(basePath, file.OwnerID.String()) {
|
||||
http.Error(w, "Invalid Path", http.StatusInternalServerError)
|
||||
app.Server.Logger.Error("invalid path")
|
||||
return
|
||||
}
|
||||
|
||||
rangeHeader := r.Header.Get("Range")
|
||||
if rangeHeader != "" {
|
||||
rangeParts := strings.Split(strings.TrimPrefix(rangeHeader, "bytes="), "-")
|
||||
if len(rangeParts) == 2 {
|
||||
start, err := strconv.ParseInt(rangeParts[0], 10, 64)
|
||||
if err != nil {
|
||||
http.Error(w, "Invalid Range", http.StatusRequestedRangeNotSatisfiable)
|
||||
return
|
||||
}
|
||||
end := int64(file.Size - 1)
|
||||
if rangeParts[1] != "" {
|
||||
end, err = strconv.ParseInt(rangeParts[1], 10, 64)
|
||||
if err != nil {
|
||||
http.Error(w, "Invalid Range", http.StatusRequestedRangeNotSatisfiable)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if end >= int64(file.Size) {
|
||||
end = int64(file.Size - 1)
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Range", fmt.Sprintf("bytes %d-%d/%d", start, end, file.Size))
|
||||
w.Header().Set("Content-Length", fmt.Sprintf("%d", end-start+1))
|
||||
w.WriteHeader(http.StatusPartialContent)
|
||||
sendFileChunk(w, saveFolder, file, start, end)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Disposition", "attachment; filename="+file.Name)
|
||||
w.Header().Set("Content-Type", "application/octet-stream")
|
||||
w.Header().Set("Accept-Ranges", "bytes")
|
||||
w.Header().Set("Content-Length", fmt.Sprintf("%d", file.Size))
|
||||
|
||||
sendFileChunk(w, saveFolder, file, 0, int64(file.Size-1))
|
||||
}
|
||||
|
||||
func sendFileChunk(w http.ResponseWriter, saveFolder string, file *models.File, start, end int64) {
|
||||
chunkSize := int64(2 * 1024 * 1024)
|
||||
|
||||
startChunk := start / chunkSize
|
||||
endChunk := end / chunkSize
|
||||
|
||||
startOffset := start % chunkSize
|
||||
endOffset := end % chunkSize
|
||||
|
||||
for i := startChunk; i <= endChunk; i++ {
|
||||
chunkPath := filepath.Join(saveFolder, file.Name, fmt.Sprintf("chunk_%d", i))
|
||||
chunkFile, err := os.Open(chunkPath)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("Error opening chunk: %v", err), http.StatusInternalServerError)
|
||||
app.Server.Logger.Error(err.Error())
|
||||
return
|
||||
}
|
||||
defer chunkFile.Close()
|
||||
|
||||
var chunkStart, chunkEnd int64
|
||||
if i == startChunk {
|
||||
chunkStart = startOffset
|
||||
} else {
|
||||
chunkStart = 0
|
||||
}
|
||||
if i == endChunk {
|
||||
chunkEnd = endOffset
|
||||
} else {
|
||||
chunkEnd = chunkSize - 1
|
||||
}
|
||||
|
||||
_, err = chunkFile.Seek(chunkStart, io.SeekStart)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("Error seeking chunk: %v", err), http.StatusInternalServerError)
|
||||
app.Server.Logger.Error(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
buffer := make([]byte, 2048)
|
||||
toSend := chunkEnd - chunkStart + 1
|
||||
for toSend > 0 {
|
||||
n, err := chunkFile.Read(buffer)
|
||||
if err != nil && err != io.EOF {
|
||||
http.Error(w, fmt.Sprintf("Error reading chunk: %v", err), http.StatusInternalServerError)
|
||||
app.Server.Logger.Error(err.Error())
|
||||
return
|
||||
}
|
||||
if n == 0 {
|
||||
break
|
||||
}
|
||||
if int64(n) > toSend {
|
||||
n = int(toSend)
|
||||
}
|
||||
_, err = w.Write(buffer[:n])
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("Error writing chunk: %v", err), http.StatusInternalServerError)
|
||||
app.Server.Logger.Error(err.Error())
|
||||
return
|
||||
}
|
||||
toSend -= int64(n)
|
||||
}
|
||||
}
|
||||
}
|
38
handler/file/file.go
Normal file
38
handler/file/file.go
Normal file
@ -0,0 +1,38 @@
|
||||
package fileHandler
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/fossyy/filekeeper/app"
|
||||
"github.com/fossyy/filekeeper/types"
|
||||
"github.com/fossyy/filekeeper/utils"
|
||||
fileView "github.com/fossyy/filekeeper/view/client/file"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func GET(w http.ResponseWriter, r *http.Request) {
|
||||
userSession := r.Context().Value("user").(types.User)
|
||||
files, err := app.Server.Database.GetFiles(userSession.UserID.String())
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
var filesData []types.FileData
|
||||
for i := 0; i < len(files); i++ {
|
||||
filesData = append(filesData, types.FileData{
|
||||
ID: files[i].ID.String(),
|
||||
Name: files[i].Name,
|
||||
Size: utils.ConvertFileSize(files[i].Size),
|
||||
Downloaded: strconv.FormatUint(files[i].Downloaded, 10),
|
||||
})
|
||||
}
|
||||
|
||||
component := fileView.Main("File Dashboard", filesData, userSession)
|
||||
err = component.Render(r.Context(), w)
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
97
handler/file/upload/upload.go
Normal file
97
handler/file/upload/upload.go
Normal file
@ -0,0 +1,97 @@
|
||||
package uploadHandler
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/fossyy/filekeeper/app"
|
||||
"github.com/fossyy/filekeeper/types"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func POST(w http.ResponseWriter, r *http.Request) {
|
||||
fileID := r.PathValue("id")
|
||||
if err := r.ParseMultipartForm(32 << 20); err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
userSession := r.Context().Value("user").(types.User)
|
||||
|
||||
uploadDir := "uploads"
|
||||
if _, err := os.Stat(uploadDir); os.IsNotExist(err) {
|
||||
if err := os.Mkdir(uploadDir, os.ModePerm); err != nil {
|
||||
app.Server.Logger.Error("error getting upload info: " + err.Error())
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
file, err := app.Server.Service.GetFile(fileID)
|
||||
if err != nil {
|
||||
app.Server.Logger.Error("error getting upload info: " + err.Error())
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
rawIndex := r.FormValue("index")
|
||||
index, err := strconv.Atoi(rawIndex)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
currentDir, err := os.Getwd()
|
||||
if err != nil {
|
||||
app.Server.Logger.Error("unable to get current directory")
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
basePath := filepath.Join(currentDir, uploadDir)
|
||||
cleanBasePath := filepath.Clean(basePath)
|
||||
|
||||
saveFolder := filepath.Join(cleanBasePath, userSession.UserID.String(), file.ID.String(), file.Name)
|
||||
|
||||
cleanSaveFolder := filepath.Clean(saveFolder)
|
||||
|
||||
if !strings.HasPrefix(cleanSaveFolder, cleanBasePath) {
|
||||
app.Server.Logger.Error("invalid path")
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if _, err := os.Stat(saveFolder); os.IsNotExist(err) {
|
||||
if err := os.MkdirAll(saveFolder, os.ModePerm); err != nil {
|
||||
app.Server.Logger.Error("error creating save folder: " + err.Error())
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
fileByte, _, err := r.FormFile("chunk")
|
||||
if err != nil {
|
||||
app.Server.Logger.Error("error getting upload info: " + err.Error())
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer fileByte.Close()
|
||||
|
||||
dst, err := os.OpenFile(filepath.Join(saveFolder, fmt.Sprintf("chunk_%d", index)), os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
|
||||
if err != nil {
|
||||
app.Server.Logger.Error("error making upload folder: " + err.Error())
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
defer dst.Close()
|
||||
if _, err := io.Copy(dst, fileByte); err != nil {
|
||||
app.Server.Logger.Error("error copying byte to file dst: " + err.Error())
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
Reference in New Issue
Block a user