Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { fileService, FileService } from "../../../core/utils/file-service";
import { CuiFileService } from "../../../core/utils/cui-file-service";
import { logger } from "../../../core/utils/logger";
import { FileConstants } from "../../../core/utils/file.constants";
import { resolve } from "node:path";

export class ActionFlowService {
public static readonly METADATA_FILE_NAME = "metadata.json";
Expand All @@ -35,9 +34,8 @@ export class ActionFlowService {
}

const fileName = "action-flows_export_" + uuidv4() + ".zip";
const fullFilePath = resolve(process.cwd(), fileName);
zip.writeZip(fullFilePath, () => fs.chmodSync(fullFilePath, FileConstants.DEFAULT_FILE_PERMISSIONS));
logger.info(FileService.fileDownloadedMessage + fileName);
const writtenFilename = await this.cuiFileService.writeZipToFileWithGivenName(zip.toBuffer(), fileName);
logger.info(FileService.fileDownloadedMessage + writtenFilename);
}

public async analyzeActionFlows(packageId: string, outputToJsonFile: boolean): Promise<void> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class BranchExportImportCommandService {
const branchPackageKey = BranchUtils.constructBranchKey(packageKey, branchKey);
const sourceDir = await this.exportRewrittenPackageDir(branchPackageKey);
try {
const message = this.writeLocalArtifact(sourceDir, packageKey, !!options.zip);
const message = await this.writeLocalArtifact(sourceDir, packageKey, !!options.zip);
if (jsonResponse) {
await this.writeJson({ packageKey: branchPackageKey, branchName: branchKey });
} else {
Expand Down Expand Up @@ -152,12 +152,11 @@ export class BranchExportImportCommandService {
return extractedDir;
}

private writeLocalArtifact(sourceDir: string, packageKey: string, zip: boolean): string {
private async writeLocalArtifact(sourceDir: string, packageKey: string, zip: boolean): Promise<string> {
if (zip) {
const zipPath = fileService.zipDirectoryAsSinglePackage(sourceDir);
try {
const fileName = `${packageKey}.zip`;
fileService.writeBufferToFileWithGivenName(fs.readFileSync(zipPath), resolve(process.cwd(), fileName));
const fileName = await this.cuiFileService.writeZipToFileWithGivenName(fs.readFileSync(zipPath), `${packageKey}.zip`);
return FileService.fileDownloadedMessage + fileName;
} finally {
fs.rmSync(zipPath, { force: true });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ import { fileService, FileService } from "../../core/utils/file-service";
import { logger } from "../../core/utils/logger";
import { GitService } from "../../core/git-profile/git/git.service";
import { SinglePackageExportApi } from "./api/single-package-export-api";
import { resolve } from "node:path";
import { CuiFileService } from "../../core/utils/cui-file-service";

export class SinglePackageExportService {

private readonly singlePackageExportApi: SinglePackageExportApi;
private readonly gitService: GitService;
private readonly cuiFileService: CuiFileService;

constructor(context: Context) {
this.singlePackageExportApi = new SinglePackageExportApi(context);
this.gitService = new GitService(context);
this.cuiFileService = new CuiFileService(context);
}

public async exportPackage(packageKey: string, zip: boolean, gitBranch: string): Promise<void> {
Expand All @@ -25,8 +27,7 @@ export class SinglePackageExportService {
}

if (zip) {
const fileName = `${packageKey}.zip`;
fileService.writeBufferToFileWithGivenName(packageData, resolve(process.cwd(), fileName));
const fileName = await this.cuiFileService.writeZipToFileWithGivenName(packageData, `${packageKey}.zip`);
logger.info(FileService.fileDownloadedMessage + fileName);
return;
}
Expand Down
3 changes: 0 additions & 3 deletions src/commands/studio/manager/space.manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,15 @@ import { BaseManager } from "../../../core/http/http-shared/base.manager";
import { ManagerConfig } from "../../../core/http/http-shared/manager-config.interface";
import { SpaceTransport } from "../interfaces/space.interface";
import { logger } from "../../../core/utils/logger";
import { CuiFileService } from "../../../core/utils/cui-file-service";

export class SpaceManager extends BaseManager {

private static BASE_URL = "/package-manager/api/spaces";

private _jsonResponse: boolean;
private readonly cuiFileService: CuiFileService;

constructor(context: Context) {
super(context);
this.cuiFileService = new CuiFileService(context);
}

public get jsonResponse(): boolean {
Expand Down
9 changes: 3 additions & 6 deletions src/commands/t2tc/t2tc-package.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import { StudioService } from "./studio.service";
import { GitService } from "../../core/git-profile/git/git.service";
import * as fs from "fs";
import { FileConstants } from "../../core/utils/file.constants";
import { resolve } from "node:path";

export class T2tcPackageService {

Expand Down Expand Up @@ -117,7 +116,7 @@ export class T2tcPackageService {
logger.info("Successfully exported packages to branch: " + gitBranch);
fs.rmSync(extractedDirectory, { recursive: true });
} else {
this.downloadZip(exportedPackagesZip, unzip);
await this.downloadZip(exportedPackagesZip, unzip);
}
}

Expand Down Expand Up @@ -243,17 +242,15 @@ export class T2tcPackageService {
return null;
}

private downloadZip(exportedZip: AdmZip, unzip: boolean): void {
private async downloadZip(exportedZip: AdmZip, unzip: boolean): Promise<void> {
if (unzip) {
const fileDownloadedMessage = "Successful download. Downloaded directory: ";
const targetDirectoryName = `export_${uuidv4()}`;
fileService.extractExportedZipWithNestedZipsToDir(exportedZip, targetDirectoryName);
logger.info(fileDownloadedMessage + targetDirectoryName);
} else {
const fileDownloadedMessage = "File downloaded successfully. New filename: ";
const filename = `export_${uuidv4()}.zip`;
const fullFilePath = resolve(process.cwd(), filename);
exportedZip.writeZip(fullFilePath, () => fs.chmodSync(fullFilePath, FileConstants.DEFAULT_FILE_PERMISSIONS));
const filename = await this.cuiFileService.writeZipToFileWithGivenName(exportedZip.toBuffer(), `export_${uuidv4()}.zip`);
logger.info(fileDownloadedMessage + filename);
}
}
Expand Down
30 changes: 13 additions & 17 deletions src/core/http/http-shared/base.manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ import { ManagerConfig } from "./manager-config.interface";
import { HttpClient } from "../http-client";
import { Context } from "../../command/cli-context";
import { FileConstants } from "../../utils/file.constants";
import { CuiFileService } from "../../utils/cui-file-service";

export abstract class BaseManager {
private httpClient: () => HttpClient;
protected readonly cuiFileService: CuiFileService;
protected readonly fileDownloadedMessage = "File downloaded successfully. New filename: ";

protected constructor(context: Context) {
this.httpClient = () => context.httpClient;
this.cuiFileService = new CuiFileService(context);
}

public async pull(): Promise<any> {
Expand All @@ -36,19 +39,14 @@ export abstract class BaseManager {
}

public async pullFile(): Promise<any> {
return new Promise<void>((resolve, reject) => {
this.httpClient()
.downloadFile(this.getConfig().pullUrl)
.then(data => {
const filename = this.writeStreamToFile(data);
logger.info(this.fileDownloadedMessage + filename);
resolve();
})
.catch(err => {
logger.error(new FatalError(err));
reject();
});
});
try {
const data = await this.httpClient().downloadFile(this.getConfig().pullUrl);
const filename = await this.writeStreamToFile(data);
logger.info(this.fileDownloadedMessage + filename);
} catch (err) {
logger.error(new FatalError(err));
throw err;
}
}

public async push(): Promise<any> {
Expand Down Expand Up @@ -98,10 +96,8 @@ export abstract class BaseManager {
return filename;
}

protected writeStreamToFile(data: any): string {
const filename = this.getConfig().exportFileName;
fs.writeFileSync(filename, data, { mode: FileConstants.DEFAULT_FILE_PERMISSIONS });
return filename;
protected async writeStreamToFile(data: Buffer): Promise<string> {
return this.cuiFileService.writeZipToFileWithGivenName(data, this.getConfig().exportFileName);
}

protected writeToFileWithGivenName(data: any, filename: string): void {
Expand Down
48 changes: 41 additions & 7 deletions src/core/utils/cui-file-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,66 @@ export class CuiFileService {
}

public async writeToFileWithGivenName(data: string, filename: string): Promise<string> {
return this.writeWithCoverHandling(data, filename, cover =>
this.writeClassifiedArchive(filename, data, cover)
);
}

public async writeZipToFileWithGivenName(zipData: Buffer, filename: string): Promise<string> {
return this.writeWithCoverHandling(zipData, filename, cover => {
const zip = new AdmZip(zipData);
this.addCoverPage(zip, cover);

return this.writeArchive(zip, filename);
});
}

private async writeWithCoverHandling(
payload: string | Buffer,
filename: string,
onClassified: (cover: CuiPdfCoverResponse) => Promise<string> | string
): Promise<string> {
const cover = await this.cuiApi.getCuiPdfCover();

if (cover === null) {
fileService.writeToFileWithGivenName(data, filename);
return filename;
if (!cover) {
return this.writePayload(payload, filename);
}

if (!this.isClassified(cover)) {
const unclassifiedName = this.prefixFileName(filename, CuiFileService.UNCLASSIFIED_PREFIX);
fileService.writeToFileWithGivenName(data, unclassifiedName);
return unclassifiedName;
return this.writePayload(payload, this.prefixFileName(filename, CuiFileService.UNCLASSIFIED_PREFIX));
}

return onClassified(cover);
}

private writePayload(payload: string | Buffer, filename: string): string {
if (Buffer.isBuffer(payload)) {
fileService.writeBufferToFileWithGivenName(payload, filename);
} else {
fileService.writeToFileWithGivenName(payload, filename);
}

return this.writeClassifiedArchive(filename, data, cover);
return filename;
}

private writeClassifiedArchive(filename: string, data: string, cover: CuiPdfCoverResponse): string {
const zip = new AdmZip();
zip.addFile(path.basename(filename), Buffer.from(data, "utf-8"), "", FileConstants.DEFAULT_FILE_PERMISSIONS);
this.addCoverPage(zip, cover);

return this.writeArchive(zip, filename);
}

private addCoverPage(zip: AdmZip, cover: CuiPdfCoverResponse): void {
zip.addFile(
CuiFileService.COVER_SHEET_FILE_NAME,
this.decodeCoverPage(cover),
"",
FileConstants.DEFAULT_FILE_PERMISSIONS
);
}

private writeArchive(zip: AdmZip, filename: string): string {
const archiveName = this.buildClassifiedArchiveName(filename);
fileService.writeBufferToFileWithGivenName(zip.toBuffer(), archiveName);

Expand Down
Loading
Loading