-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9557e8d
commit f31b276
Showing
4 changed files
with
89 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
build-logic/src/main/kotlin/org/enginehub/gradle/b2/B2Upload.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package org.enginehub.gradle.b2 | ||
|
||
import com.backblaze.b2.client.B2StorageClient | ||
import com.backblaze.b2.client.B2StorageClientFactory | ||
import com.backblaze.b2.client.contentSources.B2ContentTypes | ||
import com.backblaze.b2.client.contentSources.B2FileContentSource | ||
import com.backblaze.b2.client.structures.B2UploadFileRequest | ||
import com.google.common.hash.Hashing | ||
import com.google.common.io.Files | ||
import org.gradle.api.DefaultTask | ||
import org.gradle.api.file.DirectoryProperty | ||
import org.gradle.api.provider.Property | ||
import org.gradle.api.tasks.Input | ||
import org.gradle.api.tasks.InputDirectory | ||
import org.gradle.api.tasks.TaskAction | ||
import org.gradle.work.DisableCachingByDefault | ||
|
||
@DisableCachingByDefault(because = "Upload cannot be cached") | ||
abstract class B2Upload : DefaultTask() { | ||
/** | ||
* The directory to upload. | ||
*/ | ||
@get:InputDirectory | ||
abstract val inputDir: DirectoryProperty | ||
|
||
/** | ||
* The bucket to upload to. | ||
*/ | ||
@get:Input | ||
abstract val bucketName: Property<String> | ||
|
||
/** | ||
* The prefix to use for the files. | ||
*/ | ||
@get:Input | ||
abstract val prefix: Property<String> | ||
|
||
@TaskAction | ||
fun upload() { | ||
val client: B2StorageClient = B2StorageClientFactory.createDefaultFactory().create( | ||
"enginehub-b2-upload", | ||
) | ||
inputDir.asFileTree.visit { | ||
if (isDirectory) { | ||
return@visit | ||
} | ||
logger.lifecycle("Uploading $path to $prefix/$path") | ||
@Suppress("DEPRECATION") | ||
val sha1 = Files.asByteSource(file).hash(Hashing.sha1()) | ||
client.uploadSmallFile( | ||
B2UploadFileRequest.builder( | ||
bucketName.get(), | ||
"$prefix/$path", | ||
B2ContentTypes.APPLICATION_OCTET, | ||
B2FileContentSource.builder(file).setSha1(sha1.toString()).build() | ||
).build() | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters