Skip to content

Commit

Permalink
Merge branch 'master' of github.com:paperbits/paperbits-core
Browse files Browse the repository at this point in the history
  • Loading branch information
azaslonov committed Dec 3, 2023
2 parents 914c7f9 + f804db6 commit 22b9736
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions src/publishing/mediaPublisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@ import parallel from "await-parallel-limit";
import { maxParallelPublisingTasks } from "@paperbits/common/constants";
import { HttpClient, HttpResponse } from "@paperbits/common/http";
import { IPublisher } from "@paperbits/common/publishing";
import { IBlobStorage, Query } from "@paperbits/common/persistence";
import { Query } from "@paperbits/common/persistence";
import { IBlobStorageStream } from "@paperbits/common/persistence/IBlobStorageStream";
import { IMediaService, MediaContract, MediaVariantContract } from "@paperbits/common/media";
import { Logger } from "@paperbits/common/logging";
import { normalizePermalink } from "@paperbits/common/permalinks/utils";
import { RegExps } from "@paperbits/common";
import { ReadStream } from "node:fs";


export class MediaPublisher implements IPublisher {
constructor(
private readonly mediaService: IMediaService,
private readonly blobStorage: IBlobStorage,
private readonly outputBlobStorage: IBlobStorage,
private readonly blobStorage: IBlobStorageStream,
private readonly outputBlobStorage: IBlobStorageStream,
private readonly httpClient: HttpClient,
private readonly logger: Logger
) { }
Expand Down Expand Up @@ -59,6 +61,23 @@ export class MediaPublisher implements IPublisher {
await this.uploadToStorage(permalink, content, mediaFile.mimeType);
}

private async publishFromStorageStream(permalink: string, mediaFile: MediaVariantContract): Promise<void> {
try {
const contentStream = await this.blobStorage.getBlobAsStream(mediaFile.blobKey);

if (!contentStream) {
this.logger.trackEvent("Publishing", { message: `Blob with key ${mediaFile.blobKey} not found in source storage.` });
return null;
}

await this.outputBlobStorage.uploadStreamToBlob(permalink, <ReadStream>contentStream, mediaFile.mimeType);
}
catch (error) {
this.logger.trackEvent("Publishing", { message: `Could not download media ${mediaFile.blobKey} from source storage. ${error.message}` });
return null;
}
}

private async uploadToStorage(permalink: string, content: Uint8Array, mimeType: string): Promise<void> {
try {
await this.outputBlobStorage.uploadBlob(permalink, content, mimeType);
Expand All @@ -70,7 +89,11 @@ export class MediaPublisher implements IPublisher {

private async renderMediaFile(permalink: string, mediaFile: MediaVariantContract): Promise<void> {
if (mediaFile.blobKey) {
await this.publishFromStorage(permalink, mediaFile);
if(this.blobStorage.getBlobAsStream) {
await this.publishFromStorageStream(permalink, mediaFile);
} else {
await this.publishFromStorage(permalink, mediaFile);
}
return;
}

Expand Down

0 comments on commit 22b9736

Please sign in to comment.