Skip to content

Commit

Permalink
Fix: Use AssetDate as photo created time
Browse files Browse the repository at this point in the history
  • Loading branch information
chyroc committed Dec 29, 2023
1 parent d81ee17 commit 18a3bcc
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
19 changes: 18 additions & 1 deletion icloud-photo-cli/command/command_download.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,11 +361,28 @@ func (r *downloadCommand) downloadPhotoAssetInternal(photo *icloudgo.PhotoAsset,
tmpPath := photo.LocalPath(filepath.Join(r.Output, ".tmp"), icloudgo.PhotoVersionOriginal, r.FileStructure, livePhoto)
path := photo.LocalPath(outputDir, icloudgo.PhotoVersionOriginal, r.FileStructure, livePhoto)
name := path[len(r.Output):]

oldOutputDir := photo.OldOutputDir(r.Output, r.FolderStructure)
oldPath := photo.LocalPath(oldOutputDir, icloudgo.PhotoVersionOriginal, r.FileStructure, livePhoto)

if err := os.MkdirAll(outputDir, os.ModePerm); err != nil {
fmt.Printf("[icloudgo] [download] [%s] mkdir '%s' output dir: '%s' failed: %s\n", pickReason, photo.Filename(livePhoto), outputDir, err)
return false, err
}

// 如果 old 存在, 直接移动到新目录
if oldPath != path {
if f, _ := os.Stat(oldPath); f != nil {
if err := os.Rename(oldPath, path); err != nil {
fmt.Printf("[icloudgo] [download] [%s] compatible with wrong photo time for '%s' failed: %s\n", pickReason, name, err)
return false, err
} else {
fmt.Printf("[icloudgo] [download] [%s] compatible with wrong photo time for '%s' success\n", pickReason, name)
fmt.Printf("%s -> %s\n", oldPath, path)
}
}
}

if f, _ := os.Stat(path); f != nil {
if photo.Size() != int(f.Size()) {
return false, r.downloadTo(pickReason, photo, livePhoto, tmpPath, path, name)
Expand Down Expand Up @@ -502,7 +519,7 @@ func newAssertQueue(data []*icloudgo.PhotoAsset) *assertQueue {
recentAssets := []*icloudgo.PhotoAsset{}
oldAssets := []*icloudgo.PhotoAsset{}
for _, v := range data {
if v.Created().Before(twoDaysAge) {
if v.AssetDate().Before(twoDaysAge) {
oldAssets = append(oldAssets, v)
} else {
recentAssets = append(recentAssets, v)
Expand Down
20 changes: 17 additions & 3 deletions internal/photo_asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,31 @@ func (r *PhotoAsset) FormatSize() string {
return formatSize(r.Size())
}

func (r *PhotoAsset) Created() time.Time {
func (r *PhotoAsset) AddDate() time.Time {
return time.UnixMilli(r._masterRecord.Created.Timestamp)
}

func (r *PhotoAsset) AssetDate() time.Time {
return time.UnixMilli(r._assetRecord.Fields.AssetDate.Value)
}

func (r *PhotoAsset) OutputDir(output, folderStructure string) string {
if folderStructure == "" || folderStructure == "/" {
return output
}

createdFolderName := r.Created().Format(folderStructure)
return filepath.Join(output, createdFolderName)
assetDate := r.AssetDate().Format(folderStructure)
return filepath.Join(output, assetDate)
}

// 仅为兼容性
func (r *PhotoAsset) OldOutputDir(output, folderStructure string) string {
if folderStructure == "" || folderStructure == "/" {
return output
}

assetDate := r.AddDate().Format(folderStructure)
return filepath.Join(output, assetDate)
}

func formatSize(size int) string {
Expand Down
4 changes: 2 additions & 2 deletions internal/photo_asset_download.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ func (r *PhotoAsset) DownloadTo(version PhotoVersion, livePhoto bool, target str
}

// 1676381385791 to time.time
created := r.Created()
if err := os.Chtimes(target, created, created); err != nil {
assetDate := r.AssetDate()
if err := os.Chtimes(target, assetDate, assetDate); err != nil {
return fmt.Errorf("change file time error: %v", err)
}

Expand Down

0 comments on commit 18a3bcc

Please sign in to comment.