Skip to content

Commit

Permalink
Merge pull request #9 from wrong-kendall/main
Browse files Browse the repository at this point in the history
Add a way to mirror the directory struture when uploading a file.
  • Loading branch information
adityak74 authored Aug 7, 2022
2 parents d2a6959 + d10111b commit 647044c
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 9 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ Required: **NO**

If true, the target file name will be the complete source filename and `name` parameter will be ignored.

## ``mirrorDirectoryStructure``
Required: **NO**

If true, the directory structure of the source file will be recreated relative to ``folderId``.

## ``namePrefix``
Required: **NO**

Expand Down Expand Up @@ -90,5 +95,17 @@ jobs:
folderId: ${{ secrets.folderId }}
name: "documentation.zip" # optional string
overwrite: "true" # optional boolean
- name: Make Directory Structure
run: |
mkdir -p w/x/y
date +%s > w/x/y/z
- name: Mirror Directory Structure
uses: adityak74/google-drive-upload-git-action@main
with:
credentials: ${{ secrets.DRIVE_CREDENTIALS }}
filename: w/x/y/z
folderId: ${{ secrets.folderId }}
overwrite: "true"
mirrorDirectoryStructure: "true"

```
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ inputs:
useCompleteSourceFilenameAsName:
description: 'If true, the target file name will be the source filename and name parameter will be ignored'
required: false
mirrorDirectoryStructure:
description: 'If true, recreate the directory structure of the source file relative to the folderId'
required: false
namePrefix:
description: 'Prefix to be added to target filename'
required: false
Expand Down
81 changes: 72 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,27 @@ import (
)

const (
scope = "https://www.googleapis.com/auth/drive.file"
filenameInput = "filename"
nameInput = "name"
folderIdInput = "folderId"
credentialsInput = "credentials"
overwrite = "false"
mimeTypeInput = "mimeType"
useCompleteSourceName = "useCompleteSourceFilenameAsName"
namePrefixInput = "namePrefix"
scope = "https://www.googleapis.com/auth/drive.file"
filenameInput = "filename"
nameInput = "name"
folderIdInput = "folderId"
credentialsInput = "credentials"
overwrite = "false"
mimeTypeInput = "mimeType"
useCompleteSourceName = "useCompleteSourceFilenameAsName"
mirrorDirectoryStructure = "mirrorDirectoryStructure"
namePrefixInput = "namePrefix"
)

func uploadToDrive(svc *drive.Service, filename string, folderId string, driveFile *drive.File, name string, mimeType string) {
fi, err := os.Lstat(filename)
if err != nil {
githubactions.Fatalf(fmt.Sprintf("lstat of file with filename: %v failed with error: %v", filename, err))
}
if fi.IsDir() {
fmt.Printf("%s is a directory. skipping upload.", filename)
return
}
file, err := os.Open(filename)
if err != nil {
githubactions.Fatalf(fmt.Sprintf("opening file with filename: %v failed with error: %v", filename, err))
Expand Down Expand Up @@ -109,6 +118,14 @@ func main() {
useCompleteSourceFilenameAsNameFlag, _ = strconv.ParseBool(useCompleteSourceFilenameAsName)
}

var mirrorDirectoryStructureFlag bool
mirrorDirectoryStructure := githubactions.GetInput(mirrorDirectoryStructure)
if mirrorDirectoryStructure == "" {
fmt.Println("mirrorDirectoryStructure is disabled.")
mirrorDirectoryStructureFlag = false
} else {
mirrorDirectoryStructureFlag, _ = strconv.ParseBool(mirrorDirectoryStructure)
}
// get filename prefix
filenamePrefix := githubactions.GetInput(namePrefixInput)

Expand Down Expand Up @@ -146,8 +163,19 @@ func main() {

useSourceFilename := len(files) > 1

// Save the folderId because it might get overwritten by createDriveDirectory
originalFolderId := folderId
for _, file := range files {
folderId = originalFolderId
var targetName string
fmt.Printf("Processing file %s\n", file)
if mirrorDirectoryStructureFlag {
directoryStructure := strings.Split(filepath.Dir(file), string(os.PathSeparator))
fmt.Printf("Mirroring directory structure: %v\n", directoryStructure)
for _, dir := range directoryStructure {
folderId, err = createDriveDirectory(svc, folderId, dir)
}
}
if useCompleteSourceFilenameAsNameFlag {
targetName = file
} else if useSourceFilename || name == "" {
Expand All @@ -164,6 +192,41 @@ func main() {
}
}

func createDriveDirectory(svc *drive.Service, folderId string, name string) (string, error) {
fmt.Printf("Checking for existing folder %s\n", name)
r, err := svc.Files.List().Fields("files(name,id,mimeType,parents)").Q("name='" + name + "'" + " and mimeType='application/vnd.google-apps.folder'").IncludeItemsFromAllDrives(true).Corpora("allDrives").SupportsAllDrives(true).Do()
if err != nil {
log.Fatalf("Unable to check for folder : %v", err)
fmt.Println("Unable to check for folder")
}
foundFolders := 0
var nextFolderId string
for _, i := range r.Files {
for _, p := range i.Parents {
if p == folderId {
foundFolders++
fmt.Printf("Found existing folder %s.\n", name)
nextFolderId = i.Id
}
}
}
if foundFolders == 0 {
fmt.Printf("Creating folder: %s\n", name)
f := &drive.File{
Name: name,
MimeType: "application/vnd.google-apps.folder",
Parents: []string{folderId},
}
d, err := svc.Files.Create(f).Fields("id").SupportsAllDrives(true).Do()
if err != nil {
log.Fatalf("Unable to create folder : %v", err)
fmt.Println("Unable to create folder")
}
nextFolderId = d.Id
}
return nextFolderId, nil
}

func uploadFile(svc *drive.Service, filename string, folderId string, name string, mimeType string, overwriteFlag bool) {

fmt.Printf("target file name: %s\n", name)
Expand Down

0 comments on commit 647044c

Please sign in to comment.