Optimize file chunk validation by reducing system calls
This commit is contained in:
@ -8,7 +8,6 @@ import (
|
|||||||
"github.com/fossyy/filekeeper/utils"
|
"github.com/fossyy/filekeeper/utils"
|
||||||
fileView "github.com/fossyy/filekeeper/view/client/file"
|
fileView "github.com/fossyy/filekeeper/view/client/file"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
@ -22,17 +21,15 @@ func GET(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
var filesData []types.FileData
|
var filesData []types.FileData
|
||||||
|
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
saveFolder := filepath.Join("uploads", userSession.UserID.String(), file.ID.String(), file.Name)
|
saveFolder := filepath.Join("uploads", userSession.UserID.String(), file.ID.String(), file.Name)
|
||||||
missingChunk := false
|
|
||||||
for j := 0; j < int(file.TotalChunk); j++ {
|
|
||||||
fileName := fmt.Sprintf("%s/chunk_%d", saveFolder, j)
|
|
||||||
|
|
||||||
if _, err := os.Stat(fileName); os.IsNotExist(err) {
|
pattern := fmt.Sprintf("%s/chunk_*", saveFolder)
|
||||||
missingChunk = true
|
chunkFiles, err := filepath.Glob(pattern)
|
||||||
break
|
|
||||||
}
|
missingChunk := err != nil || len(chunkFiles) != int(file.TotalChunk)
|
||||||
}
|
|
||||||
filesData = append(filesData, types.FileData{
|
filesData = append(filesData, types.FileData{
|
||||||
ID: file.ID.String(),
|
ID: file.ID.String(),
|
||||||
Name: file.Name,
|
Name: file.Name,
|
||||||
|
@ -7,7 +7,6 @@ import (
|
|||||||
"github.com/fossyy/filekeeper/utils"
|
"github.com/fossyy/filekeeper/utils"
|
||||||
fileView "github.com/fossyy/filekeeper/view/client/file"
|
fileView "github.com/fossyy/filekeeper/view/client/file"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
@ -34,15 +33,10 @@ func PUT(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
saveFolder := filepath.Join("uploads", userSession.UserID.String(), file.ID.String(), file.Name)
|
saveFolder := filepath.Join("uploads", userSession.UserID.String(), file.ID.String(), file.Name)
|
||||||
missingChunk := false
|
pattern := fmt.Sprintf("%s/chunk_*", saveFolder)
|
||||||
for j := 0; j < int(file.TotalChunk); j++ {
|
chunkFiles, err := filepath.Glob(pattern)
|
||||||
fileName := fmt.Sprintf("%s/chunk_%d", saveFolder, j)
|
|
||||||
|
|
||||||
if _, err := os.Stat(fileName); os.IsNotExist(err) {
|
missingChunk := err != nil || len(chunkFiles) != int(file.TotalChunk)
|
||||||
missingChunk = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fileData := types.FileData{
|
fileData := types.FileData{
|
||||||
ID: file.ID.String(),
|
ID: file.ID.String(),
|
||||||
Name: file.Name,
|
Name: file.Name,
|
||||||
|
@ -15,7 +15,6 @@ import (
|
|||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@ -199,15 +198,31 @@ func handlerWS(conn *websocket.Conn, userSession types.User) {
|
|||||||
Downloaded: newFile.Downloaded,
|
Downloaded: newFile.Downloaded,
|
||||||
}
|
}
|
||||||
fileData.Chunk = make(map[string]bool)
|
fileData.Chunk = make(map[string]bool)
|
||||||
fileData.Done = true
|
|
||||||
saveFolder := filepath.Join("uploads", userSession.UserID.String(), newFile.ID.String(), newFile.Name)
|
saveFolder := filepath.Join("uploads", userSession.UserID.String(), newFile.ID.String(), newFile.Name)
|
||||||
for i := 0; i <= int(newFile.TotalChunk); i++ {
|
|
||||||
fileName := fmt.Sprintf("%s/chunk_%d", saveFolder, i)
|
pattern := fmt.Sprintf("%s/chunk_*", saveFolder)
|
||||||
if _, err := os.Stat(fileName); os.IsNotExist(err) {
|
chunkFiles, err := filepath.Glob(pattern)
|
||||||
fileData.Chunk[fmt.Sprintf("chunk_%d", i)] = false
|
if err != nil {
|
||||||
|
app.Server.Logger.Error(err.Error())
|
||||||
fileData.Done = false
|
fileData.Done = false
|
||||||
} else {
|
} else {
|
||||||
fileData.Chunk[fmt.Sprintf("chunk_%d", i)] = true
|
for i := 0; i <= int(newFile.TotalChunk); i++ {
|
||||||
|
fileData.Chunk[fmt.Sprintf("chunk_%d", i)] = false
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, chunkFile := range chunkFiles {
|
||||||
|
var chunkIndex int
|
||||||
|
fmt.Sscanf(filepath.Base(chunkFile), "chunk_%d", &chunkIndex)
|
||||||
|
|
||||||
|
fileData.Chunk[fmt.Sprintf("chunk_%d", chunkIndex)] = true
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i <= int(newFile.TotalChunk); i++ {
|
||||||
|
if !fileData.Chunk[fmt.Sprintf("chunk_%d", i)] {
|
||||||
|
fileData.Done = false
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sendSuccessResponseWithID(conn, action.Action, fileData, uploadNewFile.RequestID)
|
sendSuccessResponseWithID(conn, action.Action, fileData, uploadNewFile.RequestID)
|
||||||
@ -227,21 +242,37 @@ func handlerWS(conn *websocket.Conn, userSession types.User) {
|
|||||||
Name: file.Name,
|
Name: file.Name,
|
||||||
Size: file.Size,
|
Size: file.Size,
|
||||||
Downloaded: file.Downloaded,
|
Downloaded: file.Downloaded,
|
||||||
Done: false,
|
Chunk: make(map[string]bool),
|
||||||
|
Done: true,
|
||||||
}
|
}
|
||||||
fileData.Chunk = make(map[string]bool)
|
|
||||||
fileData.Done = true
|
|
||||||
saveFolder := filepath.Join("uploads", userSession.UserID.String(), fileData.ID.String(), fileData.Name)
|
|
||||||
for i := 0; i <= int(file.TotalChunk-1); i++ {
|
|
||||||
fileName := fmt.Sprintf("%s/chunk_%d", saveFolder, i)
|
|
||||||
|
|
||||||
if _, err := os.Stat(fileName); os.IsNotExist(err) {
|
saveFolder := filepath.Join("uploads", userSession.UserID.String(), fileData.ID.String(), fileData.Name)
|
||||||
fileData.Chunk[fmt.Sprintf("chunk_%d", i)] = false
|
pattern := fmt.Sprintf("%s/chunk_*", saveFolder)
|
||||||
|
chunkFiles, err := filepath.Glob(pattern)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
app.Server.Logger.Error(err.Error())
|
||||||
fileData.Done = false
|
fileData.Done = false
|
||||||
} else {
|
} else {
|
||||||
fileData.Chunk[fmt.Sprintf("chunk_%d", i)] = true
|
for i := 0; i <= int(file.TotalChunk); i++ {
|
||||||
|
fileData.Chunk[fmt.Sprintf("chunk_%d", i)] = false
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, chunkFile := range chunkFiles {
|
||||||
|
var chunkIndex int
|
||||||
|
fmt.Sscanf(filepath.Base(chunkFile), "chunk_%d", &chunkIndex)
|
||||||
|
|
||||||
|
fileData.Chunk[fmt.Sprintf("chunk_%d", chunkIndex)] = true
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i <= int(file.TotalChunk); i++ {
|
||||||
|
if !fileData.Chunk[fmt.Sprintf("chunk_%d", i)] {
|
||||||
|
fileData.Done = false
|
||||||
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
sendSuccessResponseWithID(conn, action.Action, fileData, uploadNewFile.RequestID)
|
sendSuccessResponseWithID(conn, action.Action, fileData, uploadNewFile.RequestID)
|
||||||
continue
|
continue
|
||||||
case Ping:
|
case Ping:
|
||||||
|
Reference in New Issue
Block a user