Skip to content

Commit

Permalink
apply the mtime given by the gopro to the written files (#126)
Browse files Browse the repository at this point in the history
This allows applications like Google Photos to have the actual timestamp
available, when the file was written, instead of reading the timestamp
when the files were downloaded from the camera.
  • Loading branch information
klaernie authored Sep 4, 2024
1 parent 3ae07d9 commit 3b595fe
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
20 changes: 12 additions & 8 deletions pkg/gopro/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func ImportConnect(params utils.ImportParams) (*utils.Result, error) {
switch fileTypeMatch.Type {
case Video, ChapteredVideo:

go func(in, folder, origFilename, unsorted string, origSize int64, lrvSize int, bar *mpb.Bar) {
go func(in, folder, origFilename, unsorted string, origSize int64, lrvSize int, bar *mpb.Bar, mtime time.Time) {
defer wg.Done()
x := origFilename
filename := origFilename
Expand All @@ -249,7 +249,8 @@ func ImportConnect(params utils.ImportParams) (*utils.Result, error) {
err := utils.DownloadFile(
filepath.Join(unsorted, origFilename),
fmt.Sprintf("http://%s:8080/videos/DCIM/%s/%s", in, folder, origFilename),
bar)
bar,
&mtime)
if err != nil {
bar.EwmaSetCurrent(origSize, 1*time.Millisecond)
bar.EwmaIncrInt64(origSize, 1*time.Millisecond)
Expand Down Expand Up @@ -304,7 +305,8 @@ func ImportConnect(params utils.ImportParams) (*utils.Result, error) {
err := utils.DownloadFile(
filepath.Join(unsorted, proxyVideoName),
fmt.Sprintf("http://%s:8080/videos/DCIM/%s/%s", in, folder, proxyVideoName),
proxyVideoBar)
proxyVideoBar,
&mtime)
if err != nil {
proxyVideoBar.EwmaSetCurrent(int64(lrvSize), 1*time.Millisecond)
proxyVideoBar.EwmaIncrInt64(int64(lrvSize), 1*time.Millisecond)
Expand All @@ -323,7 +325,7 @@ func ImportConnect(params utils.ImportParams) (*utils.Result, error) {
}
inlineCounter.SetSuccess()
}
}(params.Input, folder.D, goprofile.N, unsorted, goprofile.S, goprofile.Glrv, bar)
}(params.Input, folder.D, goprofile.N, unsorted, goprofile.S, goprofile.Glrv, bar, tm)

case Photo:
type photo struct {
Expand Down Expand Up @@ -363,13 +365,14 @@ func ImportConnect(params utils.ImportParams) (*utils.Result, error) {
}

for _, item := range totalPhotos {
go func(in string, nowPhoto photo, unsorted string) {
go func(in string, nowPhoto photo, unsorted string, mtime time.Time) {
defer wg.Done()

err := utils.DownloadFile(
filepath.Join(unsorted, nowPhoto.Name),
fmt.Sprintf("http://%s:8080/videos/DCIM/%s/%s", in, nowPhoto.Folder, nowPhoto.Name),
nowPhoto.Bar,
&mtime,
)
if err != nil {
nowPhoto.Bar.EwmaSetCurrent(int64(nowPhoto.Size), 1*time.Millisecond)
Expand All @@ -396,7 +399,7 @@ func ImportConnect(params utils.ImportParams) (*utils.Result, error) {
return
}
}
}(params.Input, item, unsorted)
}(params.Input, item, unsorted, tm)
}

case Multishot:
Expand All @@ -415,13 +418,14 @@ func ImportConnect(params utils.ImportParams) (*utils.Result, error) {
}
multiShotBar := utils.GetNewBar(progressBar, gpFileInfo.S, filename, utils.IoTX)

go func(in, folder, origFilename, unsorted string, origSize int64) {
go func(in, folder, origFilename, unsorted string, origSize int64, mtime time.Time) {
defer wg.Done()

err := utils.DownloadFile(
filepath.Join(unsorted, origFilename),
fmt.Sprintf("http://%s:8080/videos/DCIM/%s/%s", in, folder, origFilename),
multiShotBar,
&mtime,
)
if err != nil {
bar.EwmaSetCurrent(origSize, 1*time.Millisecond)
Expand All @@ -442,7 +446,7 @@ func ImportConnect(params utils.ImportParams) (*utils.Result, error) {
return
}
}
}(params.Input, folder.D, filename, unsorted, gpFileInfo.S)
}(params.Input, folder.D, filename, unsorted, gpFileInfo.S, tm)
}

default:
Expand Down
2 changes: 1 addition & 1 deletion pkg/gopro/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func UpdateCamera(sdcard string) error {
color.Yellow(">> Firmware release date: %s", camera.ReleaseDate)
color.Yellow(html2text.HTML2Text(camera.ReleaseHTML))

err = utils.DownloadFile(filepath.Join(sdcard, "UPDATE.zip"), camera.URL, nil)
err = utils.DownloadFile(filepath.Join(sdcard, "UPDATE.zip"), camera.URL, nil, nil)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/insta360/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func UpdateCamera(sdcard string, model string) error {
color.White(html2text.HTML2Text(item.Description))

fwURL := item.Channels[0].DownloadURL
err = utils.DownloadFile(filepath.Join(sdcard, strings.Split(fwURL, "/")[len(strings.Split(fwURL, "/"))-1]), fwURL, nil)
err = utils.DownloadFile(filepath.Join(sdcard, strings.Split(fwURL, "/")[len(strings.Split(fwURL, "/"))-1]), fwURL, nil, nil)
if err != nil {
return err
}
Expand Down
9 changes: 8 additions & 1 deletion pkg/utils/cameras.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func (wc WriteCounter) PrintProgress() {
fmt.Printf("\rDownloading... %s complete", humanize.Bytes(wc.Total))
}

func DownloadFile(filepath string, url string, progressbar *mpb.Bar) error {
func DownloadFile(filepath string, url string, progressbar *mpb.Bar, mtime *time.Time) error {
// Create the file, but give it a tmp file extension, this means we won't overwrite a
// file until it's downloaded, but we'll remove the tmp extension once downloaded.
out, err := os.Create(filepath + ".tmp")
Expand Down Expand Up @@ -209,6 +209,13 @@ func DownloadFile(filepath string, url string, progressbar *mpb.Bar) error {
// Close the file without defer so it can happen before Rename()
out.Close()

if mtime != nil {
// set file mtime to what was given to us
if err := os.Chtimes(filepath+".tmp", time.Time{}, *mtime); err != nil {
return err
}
}

return os.Rename(filepath+".tmp", filepath)
}

Expand Down

0 comments on commit 3b595fe

Please sign in to comment.