change ctor

This commit is contained in:
Trym Lund Flogard 2021-06-02 21:08:44 +02:00
parent cfb60b9a7f
commit c1ee7f2095
2 changed files with 15 additions and 24 deletions

View File

@ -25,7 +25,7 @@ export class NextcloudArtifact {
private async uploadFiles(files: { filesToUpload: string[]; rootDirectory: string; }) { private async uploadFiles(files: { filesToUpload: string[]; rootDirectory: string; }) {
this.logUpload(files.filesToUpload.length, files.rootDirectory); this.logUpload(files.filesToUpload.length, files.rootDirectory);
const client = new NextcloudClient(Inputs.Endpoint, this.name, files.rootDirectory); const client = new NextcloudClient(Inputs.Endpoint, this.name, files.rootDirectory, Inputs.Username, Inputs.Password);
await client.uploadFiles(files.filesToUpload); await client.uploadFiles(files.filesToUpload);
} }

View File

@ -3,13 +3,10 @@ import * as path from 'path'
import * as core from '@actions/core'; import * as core from '@actions/core';
import * as os from 'os'; import * as os from 'os';
import * as archiver from 'archiver'; import * as archiver from 'archiver';
import { URL } from 'url';
import fetch, { HeadersInit } from 'node-fetch'; import fetch, { HeadersInit } from 'node-fetch';
import { Inputs } from '../Inputs';
import btoa from 'btoa'; import btoa from 'btoa';
import { v4 as uuidv4 } from 'uuid'; import { v4 as uuidv4 } from 'uuid';
import * as webdav from 'webdav' import * as webdav from 'webdav'
import { AuthType } from 'webdav';
const fs = fsSync.promises; const fs = fsSync.promises;
@ -26,12 +23,14 @@ export class NextcloudClient {
public constructor( public constructor(
private endpoint: string, private endpoint: string,
private artifact: string, private artifact: string,
private rootDirectory: string) { private rootDirectory: string,
private username: string,
private password: string) {
this.guid = uuidv4(); this.guid = uuidv4();
this.headers = { 'Authorization': 'Basic ' + btoa(`${Inputs.Username}:${Inputs.Password}`) }; this.headers = { 'Authorization': 'Basic ' + btoa(`${this.username}:${this.password}`) };
this.davClient = webdav.createClient(`${this.endpoint}/remote.php/dav/files/${Inputs.Username}`, { this.davClient = webdav.createClient(`${this.endpoint}/remote.php/dav/files/${this.username}`, {
username: Inputs.Username, username: this.username,
password: Inputs.Password, password: this.password,
}); });
} }
@ -164,28 +163,20 @@ export class NextcloudClient {
const remoteFilePath = `${remoteFileDir}/${this.artifact}.zip`; const remoteFilePath = `${remoteFileDir}/${this.artifact}.zip`;
core.info(`Transferring file... (${file})`); core.info(`Transferring file... (${file})`);
await this.transferFile(remoteFilePath, file); const fileStat = await fs.stat(file);
core.info("finish");
return remoteFilePath;
}
private transferFile(remoteFilePath: string, file: string): Promise<[void, void]> {
const fileStream = fsSync.createReadStream(file); const fileStream = fsSync.createReadStream(file);
const fileStreamPromise = new Promise<void>((resolve, reject) => { const remoteStream = this.davClient.createWriteStream(remoteFilePath, {
fileStream.on('error', () => reject("Failed to read file")) headers: { "Content-Length": fileStat.size.toString() },
.on('end', () => resolve());
}); });
const remoteStream = this.davClient.createWriteStream(remoteFilePath);
fileStream.pipe(remoteStream); fileStream.pipe(remoteStream);
const remoteStreamPromise = new Promise<void>((resolve, reject) => { await new Promise<void>((resolve, reject) => {
remoteStream.on('error', () => reject("Failed to upload file")) fileStream.on('error', e => reject(e))
.on('close', () => resolve()); .on('finish', () => resolve());
}); });
return Promise.all([remoteStreamPromise, fileStreamPromise]); return remoteFilePath;
} }
private async shareFile(remoteFilePath: string) { private async shareFile(remoteFilePath: string) {