Skip to content

Commit 3b595fe

Browse files
authored
apply the mtime given by the gopro to the written files (#126)
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.
1 parent 3ae07d9 commit 3b595fe

File tree

4 files changed

+22
-11
lines changed

4 files changed

+22
-11
lines changed

pkg/gopro/connect.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ func ImportConnect(params utils.ImportParams) (*utils.Result, error) {
234234
switch fileTypeMatch.Type {
235235
case Video, ChapteredVideo:
236236

237-
go func(in, folder, origFilename, unsorted string, origSize int64, lrvSize int, bar *mpb.Bar) {
237+
go func(in, folder, origFilename, unsorted string, origSize int64, lrvSize int, bar *mpb.Bar, mtime time.Time) {
238238
defer wg.Done()
239239
x := origFilename
240240
filename := origFilename
@@ -249,7 +249,8 @@ func ImportConnect(params utils.ImportParams) (*utils.Result, error) {
249249
err := utils.DownloadFile(
250250
filepath.Join(unsorted, origFilename),
251251
fmt.Sprintf("http://%s:8080/videos/DCIM/%s/%s", in, folder, origFilename),
252-
bar)
252+
bar,
253+
&mtime)
253254
if err != nil {
254255
bar.EwmaSetCurrent(origSize, 1*time.Millisecond)
255256
bar.EwmaIncrInt64(origSize, 1*time.Millisecond)
@@ -304,7 +305,8 @@ func ImportConnect(params utils.ImportParams) (*utils.Result, error) {
304305
err := utils.DownloadFile(
305306
filepath.Join(unsorted, proxyVideoName),
306307
fmt.Sprintf("http://%s:8080/videos/DCIM/%s/%s", in, folder, proxyVideoName),
307-
proxyVideoBar)
308+
proxyVideoBar,
309+
&mtime)
308310
if err != nil {
309311
proxyVideoBar.EwmaSetCurrent(int64(lrvSize), 1*time.Millisecond)
310312
proxyVideoBar.EwmaIncrInt64(int64(lrvSize), 1*time.Millisecond)
@@ -323,7 +325,7 @@ func ImportConnect(params utils.ImportParams) (*utils.Result, error) {
323325
}
324326
inlineCounter.SetSuccess()
325327
}
326-
}(params.Input, folder.D, goprofile.N, unsorted, goprofile.S, goprofile.Glrv, bar)
328+
}(params.Input, folder.D, goprofile.N, unsorted, goprofile.S, goprofile.Glrv, bar, tm)
327329

328330
case Photo:
329331
type photo struct {
@@ -363,13 +365,14 @@ func ImportConnect(params utils.ImportParams) (*utils.Result, error) {
363365
}
364366

365367
for _, item := range totalPhotos {
366-
go func(in string, nowPhoto photo, unsorted string) {
368+
go func(in string, nowPhoto photo, unsorted string, mtime time.Time) {
367369
defer wg.Done()
368370

369371
err := utils.DownloadFile(
370372
filepath.Join(unsorted, nowPhoto.Name),
371373
fmt.Sprintf("http://%s:8080/videos/DCIM/%s/%s", in, nowPhoto.Folder, nowPhoto.Name),
372374
nowPhoto.Bar,
375+
&mtime,
373376
)
374377
if err != nil {
375378
nowPhoto.Bar.EwmaSetCurrent(int64(nowPhoto.Size), 1*time.Millisecond)
@@ -396,7 +399,7 @@ func ImportConnect(params utils.ImportParams) (*utils.Result, error) {
396399
return
397400
}
398401
}
399-
}(params.Input, item, unsorted)
402+
}(params.Input, item, unsorted, tm)
400403
}
401404

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

418-
go func(in, folder, origFilename, unsorted string, origSize int64) {
421+
go func(in, folder, origFilename, unsorted string, origSize int64, mtime time.Time) {
419422
defer wg.Done()
420423

421424
err := utils.DownloadFile(
422425
filepath.Join(unsorted, origFilename),
423426
fmt.Sprintf("http://%s:8080/videos/DCIM/%s/%s", in, folder, origFilename),
424427
multiShotBar,
428+
&mtime,
425429
)
426430
if err != nil {
427431
bar.EwmaSetCurrent(origSize, 1*time.Millisecond)
@@ -442,7 +446,7 @@ func ImportConnect(params utils.ImportParams) (*utils.Result, error) {
442446
return
443447
}
444448
}
445-
}(params.Input, folder.D, filename, unsorted, gpFileInfo.S)
449+
}(params.Input, folder.D, filename, unsorted, gpFileInfo.S, tm)
446450
}
447451

448452
default:

pkg/gopro/update.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func UpdateCamera(sdcard string) error {
5757
color.Yellow(">> Firmware release date: %s", camera.ReleaseDate)
5858
color.Yellow(html2text.HTML2Text(camera.ReleaseHTML))
5959

60-
err = utils.DownloadFile(filepath.Join(sdcard, "UPDATE.zip"), camera.URL, nil)
60+
err = utils.DownloadFile(filepath.Join(sdcard, "UPDATE.zip"), camera.URL, nil, nil)
6161
if err != nil {
6262
return err
6363
}

pkg/insta360/update.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func UpdateCamera(sdcard string, model string) error {
4242
color.White(html2text.HTML2Text(item.Description))
4343

4444
fwURL := item.Channels[0].DownloadURL
45-
err = utils.DownloadFile(filepath.Join(sdcard, strings.Split(fwURL, "/")[len(strings.Split(fwURL, "/"))-1]), fwURL, nil)
45+
err = utils.DownloadFile(filepath.Join(sdcard, strings.Split(fwURL, "/")[len(strings.Split(fwURL, "/"))-1]), fwURL, nil, nil)
4646
if err != nil {
4747
return err
4848
}

pkg/utils/cameras.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ func (wc WriteCounter) PrintProgress() {
172172
fmt.Printf("\rDownloading... %s complete", humanize.Bytes(wc.Total))
173173
}
174174

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

212+
if mtime != nil {
213+
// set file mtime to what was given to us
214+
if err := os.Chtimes(filepath+".tmp", time.Time{}, *mtime); err != nil {
215+
return err
216+
}
217+
}
218+
212219
return os.Rename(filepath+".tmp", filepath)
213220
}
214221

0 commit comments

Comments
 (0)