Skip to content

Commit

Permalink
fix(api-file-manager): fetch file through CMS storage ops [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel910 committed Jan 29, 2024
1 parent f494a04 commit 0c9e394
Showing 1 changed file with 34 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { NotFoundError } from "@webiny/handler-graphql";
import { File, FileManagerContext } from "~/types";
import { Asset, AssetProcessor, AssetRequest } from "~/delivery";
import { AssetAuthorizer } from "./AssetAuthorizer";
import { NotAuthorizedOutputStrategy } from "./NotAuthorizedOutputStrategy";
import { internalIdentity } from "./internalIdentity";
import { RedirectToPublicUrlOutputStrategy } from "./RedirectToPublicUrlOutputStrategy";
import { RedirectToPrivateUrlOutputStrategy } from "./RedirectToPrivateUrlOutputStrategy";
import { PrivateCache } from "./PrivateCache";
import { PublicCache } from "./PublicCache";
import { entryFromStorageTransform } from "@webiny/api-headless-cms";

interface MaybePrivate {
private?: boolean;
Expand All @@ -29,12 +30,9 @@ export class PrivateFilesAssetProcessor implements AssetProcessor {

async process(assetRequest: AssetRequest, asset: Asset): Promise<Asset> {
const id = asset.getId();
const { security } = this.context;

// Get file from File Manager by `id`.
const file = await security.withIdentity(internalIdentity, () => {
return security.withoutAuthorization(() => this.context.fileManager.getFile(id));
});
const file = await this.getFileById(id);

const isPrivateFile = this.isPrivate(file);

Expand All @@ -48,6 +46,8 @@ export class PrivateFilesAssetProcessor implements AssetProcessor {
return asset;
}

console.log("file", file);

try {
await this.assetAuthorizer.authorize(file);
} catch (error) {
Expand All @@ -65,6 +65,35 @@ export class PrivateFilesAssetProcessor implements AssetProcessor {
return processedAsset;
}

/**
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
* This method performs a very awkward data loading and type cast which should be removed as soon as we resolve the FLP issue!
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
*/
private async getFileById(id: string): Promise<File> {
const model = await this.context.security.withoutAuthorization(() => {
return this.context.cms.getModel("fmFile");
});

if (!model) {
throw new NotFoundError("File model not found!");
}

const entries = this.context.cms.storageOperations.entries;

const storageEntry = await entries.getLatestRevisionByEntryId(model, {
id
});

if (!storageEntry) {
throw new NotFoundError("File not found!");
}

const file = await entryFromStorageTransform(this.context, model, storageEntry);

return file.values as unknown as File;
}

private isPrivate(file: File) {
return file.accessControl && file.accessControl.type.startsWith("private-");
}
Expand Down

0 comments on commit 0c9e394

Please sign in to comment.