Skip to content
This repository has been archived by the owner on Sep 3, 2020. It is now read-only.

Commit

Permalink
stronger IsGoogleDoc check based on MimeType
Browse files Browse the repository at this point in the history
  • Loading branch information
Emmanuel Odeke committed Dec 5, 2014
1 parent 6e3ec61 commit 2771355
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 20 deletions.
2 changes: 1 addition & 1 deletion changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (g *Commands) resolveChangeListRecv(
if isPush {
// Handle the case of doc files for which we don't have a direct download
// url but have exportable links. These files should not be clobbered on the cloud
if r != nil && !r.IsDir && r.BlobAt == "" {
if IsGoogleDoc(r) {
return cl, nil
}

Expand Down
21 changes: 2 additions & 19 deletions pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,6 @@ const (
maxNumOfConcPullTasks = 4
)

func docExportsMap() *map[string][]string {
return &map[string][]string {
"text/plain": []string{"text/plain", "txt",},
"application/vnd.google-apps.drawing": []string{"image/svg+xml", "svg+xml",},
"application/vnd.google-apps.spreadsheet": []string{
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "xlsx",
},
"application/vnd.google-apps.document": []string{
"application/vnd.openxmlformats-officedocument.wordprocessingml.document", "docx",
},
"application/vnd.google-apps.presentation": []string{
"application/vnd.openxmlformats-officedocument.presentationml.presentation", "pptx",
},
}
}

// Pull from remote if remote path exists and in a gd context. If path is a
// directory, it recursively pulls from the remote if there are remote changes.
// It doesn't check if there are remote changes if isForce is set.
Expand Down Expand Up @@ -162,12 +146,11 @@ func (g *Commands) download(change *Change, exportOnBackup bool) (err error) {
// exportable type since we cannot directly download the raw data.
// We also need to pay attention and add the exported extension
// to avoid overriding the original file on re-syncing.
if len(change.Src.BlobAt) < 1 && exportOnBackup {
if len(change.Src.BlobAt) < 1 && exportOnBackup && IsGoogleDoc(change.Src) {
var ok bool
var mimeKeyExtList[]string

exportsMap := *docExportsMap()
mimeKeyExtList, ok = exportsMap[change.Src.MimeType]
mimeKeyExtList, ok = docExportsMap[change.Src.MimeType]
if !ok {
mimeKeyExtList = []string{"text/plain", "txt"}
}
Expand Down
30 changes: 30 additions & 0 deletions remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ var (
ErrPathNotExists = errors.New("remote path doesn't exist")
)

var docExportsMap = *newDocExportsMap()

type Remote struct {
transport *oauth.Transport
service *drive.Service
Expand All @@ -57,6 +59,18 @@ func NewRemoteContext(context *config.Context) *Remote {
return &Remote{service: service, transport: transport}
}

func IsGoogleDoc(f *File) bool {
if f == nil || f.IsDir {
return false;
}

_, ok := docExportsMap[f.MimeType]
if !ok {
return f.BlobAt == "";
}
return true;
}

func RetrieveRefreshToken(context *config.Context) (string, error) {
transport := newTransport(context)
url := transport.Config.AuthCodeURL("")
Expand Down Expand Up @@ -203,3 +217,19 @@ func newTransport(context *config.Context) *oauth.Transport {
},
}
}

func newDocExportsMap() *map[string][]string {
return &map[string][]string {
"text/plain": []string{"text/plain", "txt",},
"application/vnd.google-apps.drawing": []string{"image/svg+xml", "svg+xml",},
"application/vnd.google-apps.spreadsheet": []string{
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "xlsx",
},
"application/vnd.google-apps.document": []string{
"application/vnd.openxmlformats-officedocument.wordprocessingml.document", "docx",
},
"application/vnd.google-apps.presentation": []string{
"application/vnd.openxmlformats-officedocument.presentationml.presentation", "pptx",
},
}
}

0 comments on commit 2771355

Please sign in to comment.