mirror of
https://github.com/ente-io/ente.git
synced 2025-08-14 02:07:33 +00:00
[server] Copy files in parallel
This commit is contained in:
@@ -12,6 +12,7 @@ import (
|
|||||||
"github.com/gin-contrib/requestid"
|
"github.com/gin-contrib/requestid"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -133,12 +134,29 @@ func (fc *FileCopyController) CopyFiles(c *gin.Context, req ente.CopyFileSyncReq
|
|||||||
fileCopyList = append(fileCopyList, fileCopy)
|
fileCopyList = append(fileCopyList, fileCopy)
|
||||||
}
|
}
|
||||||
oldToNewFileIDMap := make(map[int64]int64)
|
oldToNewFileIDMap := make(map[int64]int64)
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
errChan := make(chan error, len(fileCopyList))
|
||||||
|
|
||||||
for _, fileCopy := range fileCopyList {
|
for _, fileCopy := range fileCopyList {
|
||||||
|
wg.Add(1)
|
||||||
|
go func(fileCopy fileCopyInternal) {
|
||||||
|
defer wg.Done()
|
||||||
newFile, err := fc.createCopy(c, fileCopy, userID, app)
|
newFile, err := fc.createCopy(c, fileCopy, userID, app)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
errChan <- err
|
||||||
|
return
|
||||||
}
|
}
|
||||||
oldToNewFileIDMap[fileCopy.SourceFile.ID] = newFile.ID
|
oldToNewFileIDMap[fileCopy.SourceFile.ID] = newFile.ID
|
||||||
|
}(fileCopy)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait for all goroutines to finish
|
||||||
|
wg.Wait()
|
||||||
|
|
||||||
|
// Close the error channel and check if there were any errors
|
||||||
|
close(errChan)
|
||||||
|
if err, ok := <-errChan; ok {
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
return &ente.CopyResponse{OldToNewFileIDMap: oldToNewFileIDMap}, nil
|
return &ente.CopyResponse{OldToNewFileIDMap: oldToNewFileIDMap}, nil
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user