Skip to content

Commit

Permalink
fix: issue#17 (#18)
Browse files Browse the repository at this point in the history
* add service to check whether a filename exist in array of filenames

* feat: only upload files that do not exist yet in the cloud
  • Loading branch information
mdanialr authored May 8, 2022
1 parent c34d3b9 commit f4c24e1
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 11 deletions.
40 changes: 29 additions & 11 deletions internal/provider/gdrive/gdrive.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/mdanialr/cron-upload/internal/config"
"github.com/mdanialr/cron-upload/internal/provider/gdrive/token"
"github.com/mdanialr/cron-upload/internal/scan"
"github.com/mdanialr/cron-upload/internal/service"
"golang.org/x/oauth2"
"google.golang.org/api/drive/v3"
"google.golang.org/api/option"
Expand Down Expand Up @@ -175,27 +176,44 @@ func GoogleDrive(conf *config.Model) error {
}
}

// 9. Index all files in Google Drive then compare it to soon to be uploaded files, ONLY upload
// files that do not exist yet.
var listOfFilesInCloud []string
query := fmt.Sprintf("'%s' in parents and %s != '%s'",
currentParentIdFolder, FieldMIME, MIMEFolder,
)
list, _ := dr.Files.List().Q(query).Fields("files(name)").Do()
for _, fl := range list.Files {
listOfFilesInCloud = append(listOfFilesInCloud, fl.Name)
}

// Then we are ready to upload the files to Google Drive's folder
for _, fl := range allFiles {
flInstance, err := os.Open(fl)
if err != nil {
return fmt.Errorf("failed to open file: %s with error: %s\n", fl, err)
}
defer flInstance.Close()
fl := &drive.File{
Parents: []string{currentParentIdFolder},
Name: filepath.Base(flInstance.Name()),
}
uploadFl, err := dr.Files.Create(fl).Media(flInstance).Fields(
FieldId, FieldMIME,
FieldName, FieldParents,
).Do()
if err != nil {
return fmt.Errorf("failed to upload file: %s with error: %s\n", uploadFl.Name, err)

// If this filename already exist in cloud then do not upload this file. ONLY upload filename
// that does not exist in the cloud yet.
fileName := filepath.Base(flInstance.Name())
if !service.Contains(listOfFilesInCloud, fileName) {
fl := &drive.File{
Parents: []string{currentParentIdFolder},
Name: fileName,
}
uploadFl, err := dr.Files.Create(fl).Media(flInstance).Fields(
FieldId, FieldMIME,
FieldName, FieldParents,
).Do()
if err != nil {
return fmt.Errorf("failed to upload file: %s with error: %s\n", uploadFl.Name, err)
}
}
}

// 9. Lastly, delete all soon to be deleted files using their id
// 10. Lastly, delete all soon to be deleted files using their id
for _, filesToDelete := range soonToBeDeletedFiles {
if err := dr.Files.Delete(filesToDelete).Do(); err != nil {
return fmt.Errorf("failed to delete a file or a folder with id: %s and error: %s\n", filesToDelete, err)
Expand Down
11 changes: 11 additions & 0 deletions internal/service/contain_string.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package service

// Contains tells whether b exist in a.
func Contains(a []string, b string) bool {
for _, n := range a {
if b == n {
return true
}
}
return false
}
36 changes: 36 additions & 0 deletions internal/service/contain_string_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package service

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestContains(t *testing.T) {
testCases := []struct {
name string
sample []string
check string
expect bool
}{
{
name: "Should true if `sample` contains string `check`",
sample: []string{"one.zip", "two.txt", "three.mp4"},
check: "two.txt",
expect: true,
},
{
name: "Should false if `sample` does not contains string `check`",
sample: []string{"one.zip", "two.txt", "three.mp4"},
check: "one.txt",
expect: false,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
out := Contains(tc.sample, tc.check)
assert.Equal(t, tc.expect, out)
})
}
}

0 comments on commit f4c24e1

Please sign in to comment.