update file

This commit is contained in:
link 2022-03-09 19:44:08 +08:00
parent 33acfababd
commit d890b16644
4 changed files with 58 additions and 28 deletions

View file

@ -39,7 +39,7 @@ func CheckPermission(src string) bool {
// IsNotExistMkDir create a directory if it does not exist
func IsNotExistMkDir(src string) error {
if notExist := CheckNotExist(src); notExist == true {
if notExist := CheckNotExist(src); notExist {
if err := MkDir(src); err != nil {
return err
}
@ -293,7 +293,7 @@ func WriteToPath(data []byte, path, name string) error {
}
//最终拼接
func SpliceFiles(dir, path string, length int) error {
func SpliceFiles(dir, path string, length int, startPoint int) error {
fullPath := path
@ -305,8 +305,8 @@ func SpliceFiles(dir, path string, length int) error {
)
defer file.Close()
bufferedWriter := bufio.NewWriter(file)
for i := 0; i < length; i++ {
data, err := ioutil.ReadFile(path + strconv.Itoa(i))
for i := 0; i < length+startPoint; i++ {
data, err := ioutil.ReadFile(dir + "/" + strconv.Itoa(i+startPoint))
if err != nil {
return err
}

View file

@ -292,29 +292,25 @@ func GetFileUpload(c *gin.Context) {
relative := c.Query("relativePath")
fileName := c.Query("filename")
size, _ := strconv.ParseInt(c.Query("totalSize"), 10, 64)
chunkNumber := c.Query("chunkNumber")
path := c.Query("path")
dirPath := ""
if fileName != relative {
dirPath := strings.TrimSuffix(relative, fileName)
dirPath = strings.TrimSuffix(relative, fileName)
file.MkDir(path + "/" + dirPath)
}
path += "/" + relative
if !file.CheckNotExist(path) {
f, _ := os.Stat(path)
if f.Size() == size {
hash := file.GetHashByContent([]byte(fileName))
tempDir := path + "/"
if len(dirPath) > 0 {
tempDir += dirPath + "/" + hash + "/" + chunkNumber
} else {
tempDir += hash + "/" + chunkNumber
}
if !file.CheckNotExist(tempDir) {
c.JSON(200, model.Result{Success: 200, Message: oasis_err2.GetMsg(oasis_err2.FILE_ALREADY_EXISTS)})
return
}
os.Remove(path)
c.JSON(204, model.Result{Success: 204, Message: oasis_err2.GetMsg(oasis_err2.SUCCESS)})
return
}
c.JSON(204, model.Result{Success: 204, Message: oasis_err2.GetMsg(oasis_err2.SUCCESS)})
}
@ -331,25 +327,46 @@ func PostFileUpload(c *gin.Context) {
f, _, _ := c.Request.FormFile("file")
relative := c.PostForm("relativePath")
fileName := c.PostForm("filename")
totalChunks, _ := strconv.Atoi(c.DefaultPostForm("totalChunks", "0"))
chunkNumber := c.PostForm("chunkNumber")
dirPath := ""
path := c.PostForm("path")
hash := file.GetHashByContent([]byte(fileName))
if len(path) == 0 {
c.JSON(oasis_err2.INVALID_PARAMS, model.Result{Success: oasis_err2.INVALID_PARAMS, Message: oasis_err2.GetMsg(oasis_err2.INVALID_PARAMS)})
return
}
if fileName != relative {
dirPath := strings.TrimSuffix(relative, fileName)
dirPath = strings.TrimSuffix(relative, fileName)
file.MkDir(path + "/" + dirPath)
}
tempDir := path + "/"
if len(dirPath) > 0 {
tempDir += dirPath + "/" + hash
} else {
tempDir += hash
}
path += "/" + relative
if !file.CheckNotExist(path) {
if !file.CheckNotExist(tempDir + "/" + chunkNumber) {
c.JSON(oasis_err2.FILE_ALREADY_EXISTS, model.Result{Success: oasis_err2.FILE_ALREADY_EXISTS, Message: oasis_err2.GetMsg(oasis_err2.FILE_ALREADY_EXISTS)})
return
}
if totalChunks > 1 {
file.IsNotExistMkDir(tempDir)
out, _ := os.OpenFile(tempDir+"/"+chunkNumber, os.O_WRONLY|os.O_CREATE, 0644)
defer out.Close()
_, err := io.Copy(out, f)
if err != nil {
c.JSON(oasis_err2.ERROR, model.Result{Success: oasis_err2.ERROR, Message: oasis_err2.GetMsg(oasis_err2.ERROR), Data: err.Error()})
return
}
} else {
out, _ := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0644)
defer out.Close()
_, err := io.Copy(out, f)
@ -358,6 +375,19 @@ func PostFileUpload(c *gin.Context) {
return
}
c.JSON(http.StatusOK, model.Result{Success: oasis_err2.SUCCESS, Message: oasis_err2.GetMsg(oasis_err2.SUCCESS)})
return
}
fileNum, err := ioutil.ReadDir(tempDir)
if err != nil {
c.JSON(oasis_err2.ERROR, model.Result{Success: oasis_err2.ERROR, Message: oasis_err2.GetMsg(oasis_err2.ERROR), Data: err.Error()})
return
}
if totalChunks == len(fileNum) {
file.SpliceFiles(tempDir, path, totalChunks, 1)
file.RMDir(tempDir)
}
c.JSON(http.StatusOK, model.Result{Success: oasis_err2.SUCCESS, Message: oasis_err2.GetMsg(oasis_err2.SUCCESS)})
}
// @Summary copy or move file

View file

@ -157,7 +157,7 @@ func ReadContent(stream quic.Stream) (model.MessageModel, error) {
err = ioutil.WriteFile(filepath, dataByte, 0644)
if dataModel.Index >= (dataModel.Length - 1) {
file.SpliceFiles("", path, dataModel.Length)
//file.SpliceFiles("", path, dataModel.Length)
break
}
} else {

View file

@ -2,4 +2,4 @@ package types
const CURRENTVERSION = "0.2.10"
const BODY = "<li>sAdded CasaOS own file manager</li><li>Fixed issues</li><li>Update front-end translation</li>"
const BODY = "<li>Added CasaOS own file manager</li><li>Fixed the problem of failed to create storage space</li>"