Merge pull request #1 from t3rry327/add_decompress_feature

Add decompress feature
This commit is contained in:
Emmanuel Lampe 2023-08-07 08:42:31 +02:00 committed by GitHub
commit ba8dcf44b4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 2 deletions

View file

@ -46,7 +46,11 @@ jobs:
# hello.txt
# hello2.txt
target: "./"
# decompress-target: true
```
## File Decompression
`decompress-target` allows decompression of archive files (`.zip, .tar, .tar.gz, .tgz, .rar`) after they are uploaded to the server. If you have multiple targets, it will decompress all valid compressed ones. If this option is not provided or set to false, files will be uploaded as is, without decompression.
### Multiple File/Server Example

View file

@ -10,8 +10,8 @@ async function main() {
const settings = await getSettings();
configureAxios(settings.panelHost, settings.apiKey, settings.proxy);
const { serverIds, sourceListPath, targetPath, restart, targets } =
settings;
const { serverIds, sourceListPath, targetPath, restart, targets, decompressTarget } =
settings;
for (const serverId of serverIds) {
for (const source of sourceListPath) {
@ -20,7 +20,12 @@ async function main() {
const buffer = await fs.readFile(source);
await uploadFile(serverId, targetFile, buffer);
if (decompressTarget && isArchiveFile(targetFile)) {
await decompressFile(serverId, targetFile);
}
}
for (const element of targets) {
const { source, target } = element;
await validateSourceFile(source);
@ -28,6 +33,10 @@ async function main() {
const buffer = await fs.readFile(source);
await uploadFile(serverId, targetFile, buffer);
if (decompressTarget && isArchiveFile(targetFile)) {
await decompressFile(serverId, targetFile);
}
}
if (restart) await restartServer(serverId);
@ -44,6 +53,7 @@ async function getSettings() {
const apiKey = getInput("api-key", { required: true });
const restart = getInput("restart") == "true";
const proxy = getInput("proxy");
const decompressTarget = getInput("decompress-target") == "true";
let sourcePath = getInput("source");
let sourceListPath = getMultilineInput("sources");
@ -106,6 +116,7 @@ async function getSettings() {
targetPath,
serverIds,
targets,
decompressTarget,
};
}
@ -137,6 +148,11 @@ async function validateSourceFile(source) {
}
}
function isArchiveFile(fileName) {
const ext = path.extname(fileName).toLowerCase();
return ['.zip', '.tar', '.tar.gz', '.tgz', '.rar'].includes(ext);
}
function getTargetFile(targetPath, source) {
return targetPath.endsWith("/")
? path.join(targetPath, path.basename(source))
@ -163,6 +179,13 @@ async function restartServer(serverId) {
});
}
async function decompressFile(serverId, targetFile) {
await axios.post(`/api/client/servers/${serverId}/files/decompress`, {
root: "/",
file: targetFile,
});
}
function getInput(name, options = { required: false }) {
return core.getInput(name, { ...options, trimWhitespace: true });
}