Delete file from local when removed from remote

This commit is contained in:
Neeraj Gupta 2023-10-16 19:01:19 +05:30
parent 20563eb741
commit dd4cfc899a
3 changed files with 61 additions and 17 deletions

View file

@ -47,12 +47,27 @@ func (a *albumDiskInfo) AddEntry(metadata *export.DiskFileMetadata) error {
return nil
}
func (a *albumDiskInfo) RemoveEntry(metadata *export.DiskFileMetadata) error {
if _, ok := (*a.FileIdToDiskFileMap)[metadata.Info.ID]; !ok {
return errors.New("fileID not present")
}
if _, ok := (*a.MetaFileNameToDiskFileMap)[metadata.MetaFileName]; !ok {
return errors.New("fileName not present")
}
delete(*a.MetaFileNameToDiskFileMap, metadata.MetaFileName)
delete(*a.FileIdToDiskFileMap, metadata.Info.ID)
for _, filename := range metadata.Info.FileNames {
delete(*a.FileNames, filename)
}
return nil
}
func (a *albumDiskInfo) IsMetaFileNamePresent(metaFileName string) bool {
_, ok := (*a.MetaFileNameToDiskFileMap)[metaFileName]
return ok
}
func (a *albumDiskInfo) GetDiskFile(file model.RemoteFile) *export.DiskFileMetadata {
func (a *albumDiskInfo) GetDiskFileMetadata(file model.RemoteFile) *export.DiskFileMetadata {
// check if file.ID is present
diskFile, ok := (*a.FileIdToDiskFileMap)[file.ID]
if !ok {

View file

@ -43,8 +43,22 @@ type DiskFileMetadata struct {
MetaFileName string `json:"-"`
}
func (d *DiskFileMetadata) AddFileName(fileName string) {
if d.Info.FileNames == nil {
d.Info.FileNames = make([]string, 0)
}
for _, ownerID := range d.Info.FileNames {
if ownerID == fileName {
return
}
}
d.Info.FileNames = append(d.Info.FileNames, fileName)
}
type Info struct {
ID int64 `json:"id"`
Hash *string `json:"hash"`
OwnerID int64 `json:"ownerID"`
// A file can contain multiple parts (example: live photos or burst photos)
FileNames []string `json:"fileNames"`
}

View file

@ -37,7 +37,6 @@ func (c *ClICtrl) syncFiles(ctx context.Context) error {
if entry.SyncedLocally {
continue
}
albumInfo, ok := albumIDToMetaMap[entry.AlbumID]
if !ok {
log.Printf("Album %d not found in local metadata", entry.AlbumID)
@ -69,9 +68,11 @@ func (c *ClICtrl) syncFiles(ctx context.Context) error {
if err != nil {
return err
}
err = c.downloadEntry(ctx, albumDiskInfo, *existingEntry, entry)
if err != nil {
return err
if existingEntry.GetFileType() != model.LivePhoto && albumDiskInfo.AlbumMeta.ID == 1580559962519759 {
err = c.downloadEntry(ctx, albumDiskInfo, *existingEntry, entry)
if err != nil {
return err
}
}
} else {
log.Fatalf("remoteFile %d not found in remoteFiles", entry.FileID)
@ -86,13 +87,11 @@ func (c *ClICtrl) downloadEntry(ctx context.Context,
albumEntry *model.AlbumFileEntry) error {
if !diskInfo.AlbumMeta.IsDeleted && albumEntry.IsDeleted {
albumEntry.IsDeleted = true
diskFile := diskInfo.GetDiskFile(file)
if diskFile != nil {
// remove the file from disk
log.Printf("Removing file %s from disk", diskFile.MetaFileName)
err := os.Remove(filepath.Join(diskInfo.ExportRoot, diskInfo.AlbumMeta.FolderName, ".meta", diskFile.MetaFileName))
if err != nil {
return err
diskFileMeta := diskInfo.GetDiskFileMetadata(file)
if diskFileMeta != nil {
removeErr := removeDiskFile(diskFileMeta, diskInfo)
if removeErr != nil {
return removeErr
}
}
putErr := c.DeleteValue(ctx, model.RemoteAlbumEntries, []byte(fmt.Sprintf("%d:%d", albumEntry.AlbumID, albumEntry.FileID)))
@ -124,21 +123,37 @@ func (c *ClICtrl) downloadEntry(ctx context.Context,
if err != nil {
return err
}
fileName := filepath.Join(diskInfo.ExportRoot, diskInfo.AlbumMeta.FolderName, strings.TrimSuffix(filepath.Base(potentialDiskFileName), ".json"))
// move the decrypt file to fileName
err = os.Rename(*decrypt, fileName)
filePath := filepath.Join(diskInfo.ExportRoot, diskInfo.AlbumMeta.FolderName, strings.TrimSuffix(filepath.Base(potentialDiskFileName), ".json"))
// move the decrypt file to filePath
err = os.Rename(*decrypt, filePath)
if err != nil {
return err
}
fileDiskMetadata.AddFileName(filepath.Base(filePath))
err = writeJSONToFile(filepath.Join(diskInfo.ExportRoot, diskInfo.AlbumMeta.FolderName, ".meta", potentialDiskFileName), fileDiskMetadata)
if err != nil {
return err
}
}
return nil
}
func removeDiskFile(diskFileMeta *export.DiskFileMetadata, diskInfo *albumDiskInfo) error {
// remove the file from disk
log.Printf("Removing file %s from disk", diskFileMeta.MetaFileName)
err := os.Remove(filepath.Join(diskInfo.ExportRoot, diskInfo.AlbumMeta.FolderName, ".meta", diskFileMeta.MetaFileName))
if err != nil {
return err
}
for _, fileName := range diskFileMeta.Info.FileNames {
err = os.Remove(filepath.Join(diskInfo.ExportRoot, diskInfo.AlbumMeta.FolderName, fileName))
if err != nil {
return err
}
}
return diskInfo.RemoveEntry(diskFileMeta)
}
// readFolderMetadata reads the metadata of the files in the given path
// For disk export, a particular albums files are stored in a folder named after the album.
// Inside the folder, the files are stored at top level and its metadata is stored in a .meta folder
@ -190,7 +205,7 @@ func readFilesMetadata(home string, albumMeta *export.AlbumMetadata) (*albumDisk
continue // Skip this entry if reading fails
}
if err := json.Unmarshal(metaDataBytes, &metaData); err == nil {
metaData.DiskFileName = fileName
metaData.MetaFileName = fileName
result[fileName] = &metaData
fileIdToMetadata[metaData.Info.ID] = &metaData
}