Skip to content

Commit

Permalink
Merge branch 'release/0.3.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
lecaros committed Apr 15, 2021
2 parents 67fc028 + d2e29ad commit 0d01d62
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 7 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,16 @@ override fun getDriveService(): Drive {
* Copy full folder structure without files.
* Get a list of files inside a folder by its MimeType.
* Get file's name by its URL.
* Get file given its url.
* Export/download a file.

## To-Do
* Read responses to control execution.

## Versions
### 0.3.0
* Export/download a file.
* Get file given its url.
### 0.2.0
* Allows to copy a full folder structure.
* Add method to get files in a folder given its MimeType.
Expand All @@ -37,4 +42,4 @@ override fun getDriveService(): Drive {
* Allows to recursively copy all elements from a folder to another by their ids.
* Allows creation of a folder with a name and parent folder id.
* Gets a list of files inside a folder. Only first level children.
* Find a folder by name inside a parent folder.
* Find a folder by name inside a parent folder.
14 changes: 13 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = "com.merkenlabs.googleapiwrapper.drive"
version = "0.2.0"
version = "0.3.0"
java.sourceCompatibility = JavaVersion.VERSION_1_8

repositories {
Expand Down Expand Up @@ -42,4 +42,16 @@ publishing{
from(components["java"])
}
}
repositories {
maven {
url = uri(System.getenv("MVN_REPO_URL"))
credentials {
username = System.getenv("MVN_REPO_USER")
password = System.getenv("MVN_REPO_PWD")
}
authentication {
create<org.gradle.authentication.http.BasicAuthentication>("basic")
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package com.merkenlabs.googleapiwrapper.drive
import com.google.api.services.drive.Drive
import com.google.api.services.drive.model.File
import com.merkenlabs.googleapiwrapper.drive.AbstractDriveServiceWrapper.MimeTypes.FOLDER
import java.io.ByteArrayOutputStream
import java.io.FileOutputStream

abstract class AbstractDriveServiceWrapper : IDriveServiceWrapper {

Expand Down Expand Up @@ -32,6 +34,21 @@ abstract class AbstractDriveServiceWrapper : IDriveServiceWrapper {
return copyFileToFile(originFile, newFile)
}

override fun exportFileAs(fileId: String, fileMimeType: String, fileName: String?): java.io.File {
val outputStream = ByteArrayOutputStream()
getDriveService().files().export(fileId, fileMimeType)
.executeMediaAndDownloadTo(outputStream)
writeToFileSystem(fileName?:fileId, outputStream)
return java.io.File(fileName?:fileId)
}

private fun writeToFileSystem(fileId: String, outputStream: ByteArrayOutputStream) {
val file = FileOutputStream(fileId)
outputStream.writeTo(file)
outputStream.close()
file.close()
}

private fun copyFileToFile(
originFile: File,
destinationFile: File
Expand Down Expand Up @@ -86,11 +103,15 @@ abstract class AbstractDriveServiceWrapper : IDriveServiceWrapper {
}

override fun findFilenameByUrl(url: String): String {
val idFromUrl = url.split("google.com")[1].split("/")[3]
val file = getDriveService().files().get(idFromUrl).execute()
val file = getFileFromUrl(url)
return file.name
}

override fun getFileFromUrl(url: String): File {
val idFromUrl = url.split("google.com")[1].split("/")[3]
return getDriveService().files().get(idFromUrl).execute()
}

protected abstract fun getDriveService(): Drive

object MimeTypes {
Expand All @@ -99,5 +120,6 @@ abstract class AbstractDriveServiceWrapper : IDriveServiceWrapper {
const val SPREADSHEET = "application/vnd.google-apps.spreadsheet"
const val DOCUMENT = "application/vnd.google-apps.document"
const val PRESENTATION = "application/vnd.google-apps.presentation"
const val PDF = "application/pdf"
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.merkenlabs.googleapiwrapper.drive

import com.google.api.services.drive.Drive
import com.google.api.services.drive.model.File
import java.io.File as FSFile

interface IDriveServiceWrapper {
/**
Expand All @@ -20,4 +20,6 @@ interface IDriveServiceWrapper {
* Returns the new File.
**/
fun copyFile(originFile: File, destinationFolderId: String): File?
}
fun exportFileAs(fileId: String, fileMimeType: String, fileName: String?): FSFile
fun getFileFromUrl(url: String): File?
}

0 comments on commit 0d01d62

Please sign in to comment.