-
-
Notifications
You must be signed in to change notification settings - Fork 247
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
Showing
4 changed files
with
129 additions
and
12 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
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
95 changes: 95 additions & 0 deletions
95
buildSrc/src/main/groovy/io/izzel/arclight/gradle/tasks/UploadFilesTask.groovy
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,95 @@ | ||
package io.izzel.arclight.gradle.tasks | ||
|
||
import groovy.json.JsonOutput | ||
import io.izzel.arclight.gradle.Utils | ||
import org.gradle.api.DefaultTask | ||
import org.gradle.api.provider.Property | ||
import org.gradle.api.tasks.Input | ||
import org.gradle.api.tasks.TaskAction | ||
|
||
import java.nio.charset.StandardCharsets | ||
import java.nio.file.Files | ||
|
||
abstract class UploadFilesTask extends DefaultTask { | ||
|
||
@Input | ||
abstract Property<String> getMcVersion() | ||
|
||
@Input | ||
abstract Property<String> getVersion() | ||
|
||
@Input | ||
abstract Property<Boolean> getSnapshot() | ||
|
||
@Input | ||
abstract Property<String> getGitHash() | ||
|
||
@Input | ||
abstract Property<String> getBranch() | ||
|
||
@TaskAction | ||
void run() { | ||
for (def file in inputs.files.asFileTree.files) { | ||
if (file.isFile()) { | ||
try { | ||
this.uploadOne(file) | ||
} catch (Exception e) { | ||
project.logger.error("Error uploading $file", e) | ||
throw e | ||
} | ||
} | ||
} | ||
} | ||
|
||
private static final String OBJECTS = "https://files.hypertention.cn/v1/objects/%s" | ||
private static final String FILES = "https://files.hypertention.cn/v1/files%s" | ||
|
||
private void uploadOne(File file) { | ||
def sha1 = Utils.sha1(file) | ||
def modloader = file.name.split('-')[1] | ||
project.logger.lifecycle("Uploading {}, sha1 {}", file.name, sha1) | ||
(new URL(OBJECTS.formatted(sha1)).openConnection() as HttpURLConnection).with { | ||
it.setRequestMethod("PUT") | ||
it.doOutput = true | ||
it.addRequestProperty("X-Lightning-Sha1", sha1) | ||
it.addRequestProperty("X-Lightning-Filename", file.name.replace(".jar", "-" + gitHash.get() + ".jar")) | ||
it.addRequestProperty("AuthToken", System.getenv().ARCLIGHT_FILES_TOKEN) | ||
it.addRequestProperty("Content-Type", "application/java-archive") | ||
it.addRequestProperty("Content-Length", Files.size(file.toPath()).toString()) | ||
it.setInstanceFollowRedirects(true) | ||
it.connect() | ||
Utils.using(it.outputStream) { | ||
Files.copy(file.toPath(), it) | ||
} | ||
if (it.responseCode != 200) { | ||
def reason = new String(it.inputStream.readAllBytes()) | ||
project.logger.error(reason) | ||
throw new Exception(reason) | ||
} | ||
} | ||
link("/arclight/branches/${branch.get()}/versions-snapshot/${version.get()}/${modloader}", [type: 'object', value: sha1]) | ||
link("/arclight/branches/${branch.get()}/latest-snapshot", [type: 'link', value: "/arclight/branches/${branch.get()}/versions-snapshot/${version.get()}", cache_seconds: 3600]) | ||
if (!snapshot.get()) { | ||
link("/arclight/branches/${branch.get()}/versions-stable/${version.get()}/${modloader}", [type: 'object', value: sha1]) | ||
link("/arclight/branches/${branch.get()}/latest-stable", [type: 'link', value: "/arclight/branches/${branch.get()}/versions-stable/${version.get()}", cache_seconds: 86400]) | ||
} | ||
} | ||
|
||
private static void link(String path, Object payload) { | ||
(new URL(FILES.formatted(path)).openConnection() as HttpURLConnection).with { | ||
it.setRequestMethod("PUT") | ||
it.doOutput = true | ||
it.addRequestProperty("Content-Type", "application/json") | ||
it.addRequestProperty("AuthToken", System.getenv().ARCLIGHT_FILES_TOKEN) | ||
it.connect() | ||
Utils.using(it.outputStream) { | ||
it.write(JsonOutput.toJson(payload).getBytes(StandardCharsets.UTF_8)) | ||
} | ||
if (it.responseCode != 200) { | ||
def reason = new String(it.inputStream.readAllBytes()) | ||
project.logger.error(reason) | ||
throw new Exception(reason) | ||
} | ||
} | ||
} | ||
} |