Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#175374436] Allow lowercase transformation on png naming while uploading a Service image Logo #95

Merged
merged 6 commits into from
Oct 22, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
65 changes: 55 additions & 10 deletions UploadServiceLogo/__test__/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { none, some } from "fp-ts/lib/Option";

import { NonEmptyString } from "italia-ts-commons/lib/strings";

import { BlobService } from "azure-storage";
import { fromEither, fromLeft } from "fp-ts/lib/TaskEither";
import { toCosmosErrorResponse } from "io-functions-commons/dist/src/utils/cosmosdb_model";
import { Logo } from "../../generated/definitions/Logo";
Expand All @@ -21,6 +22,7 @@ describe("UpdateServiceLogoHandler", () => {

const updateServiceLogoHandler = UpdateServiceLogoHandler(
mockServiceModel as any,
undefined as any,
undefined as any
);
const response = await updateServiceLogoHandler(
Expand Down Expand Up @@ -48,6 +50,7 @@ describe("UpdateServiceLogoHandler", () => {

const updateServiceLogoHandler = UpdateServiceLogoHandler(
mockServiceModel as any,
undefined as any,
undefined as any
);
const response = await updateServiceLogoHandler(
Expand All @@ -72,6 +75,12 @@ describe("UpdateServiceLogoHandler", () => {
logo: undefined
}
};

const blobServiceMock = ({
createBlockBlobFromText: jest.fn((_, __, ___, cb) => {
return cb(null, "any");
})
} as any) as BlobService;
const aServiceId = "1" as NonEmptyString;
const logosUrl = "LOGOS_URL";
const mockServiceModel = {
Expand All @@ -82,6 +91,7 @@ describe("UpdateServiceLogoHandler", () => {

const updateServiceLogoHandler = UpdateServiceLogoHandler(
mockServiceModel as any,
blobServiceMock,
logosUrl
);
const response = await updateServiceLogoHandler(
Expand All @@ -103,21 +113,22 @@ describe("UpdateServiceLogoHandler", () => {
logo:
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII="
} as Logo;
const mockedContext = {
bindings: {
logo: undefined
}
};
const mockedContext = {};
const aServiceId = "1" as NonEmptyString;
const logosUrl = "LOGOS_URL";
const mockServiceModel = {
findOneByServiceId: jest.fn(() => {
return fromEither(right(some({})));
})
};

const blobServiceMock = ({
createBlockBlobFromText: jest.fn((_, __, ___, ____, cb) =>
cb(null, "any")
)
} as any) as BlobService;
const updateServiceLogoHandler = UpdateServiceLogoHandler(
mockServiceModel as any,
blobServiceMock,
logosUrl
);
const response = await updateServiceLogoHandler(
Expand All @@ -130,10 +141,44 @@ describe("UpdateServiceLogoHandler", () => {
expect(mockServiceModel.findOneByServiceId).toHaveBeenCalledWith(
aServiceId
);
expect(mockedContext.bindings.logo).toBeDefined();
expect(mockedContext.bindings.logo.toString("base64")).toEqual(
requestPayload.logo
);

expect(response.kind).toBe("IResponseSuccessRedirectToResource");
});

it("should return an internal error response if blob write fails", async () => {
const requestPayload = {
logo:
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII="
} as Logo;
const mockedContext = {};
const aServiceId = "1" as NonEmptyString;
const logosUrl = "LOGOS_URL";
const mockServiceModel = {
findOneByServiceId: jest.fn(() => {
return fromEither(right(some({})));
})
};
const blobServiceMock = ({
createBlockBlobFromText: jest.fn((_, __, ___, ____, cb) =>
cb("any", null)
)
} as any) as BlobService;
const updateServiceLogoHandler = UpdateServiceLogoHandler(
mockServiceModel as any,
blobServiceMock,
logosUrl
);
const response = await updateServiceLogoHandler(
mockedContext as any,
undefined as any, // Not used
aServiceId,
requestPayload
);

expect(mockServiceModel.findOneByServiceId).toHaveBeenCalledWith(
aServiceId
);

expect(response.kind).toBe("IResponseErrorInternal");
});
});
7 changes: 0 additions & 7 deletions UploadServiceLogo/function.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@
"type": "http",
"direction": "out",
"name": "res"
},
{
"type": "blob",
"name": "logo",
"path": "services/{serviceId}.png",
"connection": "AssetsStorageConnection",
"direction": "out"
}
],
"scriptFile": "../dist/UploadServiceLogo/index.js"
Expand Down
113 changes: 87 additions & 26 deletions UploadServiceLogo/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import { Context } from "@azure/functions";

import * as express from "express";

import { isLeft } from "fp-ts/lib/Either";
import { fromPredicate, isNone } from "fp-ts/lib/Option";
import { isLeft, left, right, toError } from "fp-ts/lib/Either";
import { isNone } from "fp-ts/lib/Option";

import {
IResponseErrorInternal,
IResponseErrorNotFound,
IResponseErrorValidation,
IResponseSuccessRedirectToResource,
ResponseErrorInternal,
ResponseErrorNotFound,
ResponseErrorValidation,
ResponseSuccessRedirectToResource
Expand All @@ -31,7 +32,18 @@ import {
ResponseErrorQuery
} from "io-functions-commons/dist/src/utils/response";

import { tryCatch } from "fp-ts/lib/Option";
import { BlobService } from "azure-storage";
import { identity } from "fp-ts/lib/function";
import { Option, tryCatch } from "fp-ts/lib/Option";
import {
fromLeft,
fromPredicate as fromPredicateT,
taskEither,
TaskEither,
tryCatch as tryCatchT
} from "fp-ts/lib/TaskEither";
import { fromEither } from "fp-ts/lib/TaskEither";
import { upsertBlobFromObject } from "io-functions-commons/dist/src/utils/azure_storage";
import * as UPNG from "upng-js";
import { Logo as ApiLogo } from "../generated/definitions/Logo";
import { ServiceId } from "../generated/definitions/ServiceId";
Expand All @@ -58,11 +70,23 @@ const imageValidationErrorResponse = () =>
"The base64 representation of the logo is invalid"
);

const tUpsertBlobFromObject = (
blobService: BlobService,
containerName: string,
blobName: string,
content: string
): TaskEither<Error, Option<BlobService.BlobResult>> =>
tryCatchT(
() => upsertBlobFromObject(blobService, containerName, blobName, content),
toError
).chain(_ => _.fold(err => fromLeft(err), opt => taskEither.of(opt)));

export function UpdateServiceLogoHandler(
serviceModel: ServiceModel,
blobService: BlobService,
logosUrl: string
): IUpdateServiceHandler {
return async (context, _, serviceId, logoPayload) => {
return async (_, __, serviceId, logoPayload) => {
const errorOrMaybeRetrievedService = await serviceModel
.findOneByServiceId(serviceId)
.run();
Expand All @@ -82,27 +106,63 @@ export function UpdateServiceLogoHandler(
}

const bufferImage = Buffer.from(logoPayload.logo, "base64");
return tryCatch(() => UPNG.decode(bufferImage)).foldL(
() => imageValidationErrorResponse(),
image =>
fromPredicate((img: UPNG.Image) => img.width > 0 && img.height > 0)(
image
).foldL<
IResponseErrorValidation | IResponseSuccessRedirectToResource<{}, {}>
>(
() => imageValidationErrorResponse(),
() => {
// tslint:disable-next-line:no-object-mutation
context.bindings.logo = bufferImage;

return ResponseSuccessRedirectToResource(
{},
`${logosUrl}/services/${serviceId}.png`,
{}
);
}
)
);
return fromEither(
tryCatch(() => UPNG.decode(bufferImage)).foldL(
() =>
left<IResponseErrorValidation, UPNG.Image>(
imageValidationErrorResponse()
),
img => right<IResponseErrorValidation, UPNG.Image>(img)
)
)
.chain(image =>
fromPredicateT(
(img: UPNG.Image) => img.width > 0 && img.height > 0,
() => imageValidationErrorResponse()
)(image)
)
.foldTaskEither<
IResponseErrorValidation | IResponseErrorInternal,
IResponseSuccessRedirectToResource<{}, {}>
>(
imageValidationError => fromLeft(imageValidationError),
() =>
tUpsertBlobFromObject(
blobService,
"services",
`${serviceId.toLowerCase()}.png`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd save this value in a variable as it's being used later in the code.

bufferImage.toString()
)
.mapLeft(err =>
ResponseErrorInternal(
`Error trying to connect to storage ${err.message}`
)
)
.chain(maybeResult =>
maybeResult.foldL(
() =>
fromLeft(
ResponseErrorInternal(
"Error trying to upload image logo on storage"
)
),
() =>
taskEither.of(
ResponseSuccessRedirectToResource(
{},
`${logosUrl}/services/${serviceId.toLowerCase()}.png`,
{}
)
)
)
)
)
.fold<
| IResponseErrorValidation
| IResponseErrorInternal
| IResponseSuccessRedirectToResource<{}, {}>
>(identity, identity)
.run();
};
}

Expand All @@ -111,9 +171,10 @@ export function UpdateServiceLogoHandler(
*/
export function UploadServiceLogo(
serviceModel: ServiceModel,
blobService: BlobService,
logosUrl: string
): express.RequestHandler {
const handler = UpdateServiceLogoHandler(serviceModel, logosUrl);
const handler = UpdateServiceLogoHandler(serviceModel, blobService, logosUrl);

const middlewaresWrap = withRequestMiddlewares(
// Extract Azure Functions bindings
Expand Down
5 changes: 4 additions & 1 deletion UploadServiceLogo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import createAzureFunctionHandler from "io-functions-express/dist/src/createAzur

import { UploadServiceLogo } from "./handler";

import { createBlobService } from "azure-storage";
import { getConfigOrThrow } from "../utils/config";
import { cosmosdbClient } from "../utils/cosmosdb";

Expand All @@ -27,6 +28,8 @@ const servicesContainer = database.container(SERVICE_COLLECTION_NAME);

const serviceModel = new ServiceModel(servicesContainer);

const blobService = createBlobService(config.AssetsStorageConnection);

// tslint:disable-next-line: no-let
let logger: Context["log"] | undefined;
const contextTransport = new AzureContextTransport(() => logger, {
Expand All @@ -41,7 +44,7 @@ secureExpressApp(app);
// Add express route
app.put(
"/adm/services/:serviceid/logo",
UploadServiceLogo(serviceModel, logosUrl)
UploadServiceLogo(serviceModel, blobService, logosUrl)
);

const azureFunctionHandler = createAzureFunctionHandler(app);
Expand Down
1 change: 1 addition & 0 deletions utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ export const IConfig = t.intersection([
USER_DATA_BACKUP_CONTAINER_NAME: NonEmptyString,
USER_DATA_CONTAINER_NAME: NonEmptyString,

AssetsStorageConnection: NonEmptyString,
StorageConnection: NonEmptyString,
SubscriptionFeedStorageConnection: NonEmptyString,
UserDataArchiveStorageConnection: NonEmptyString,
Expand Down