Skip to content

Commit 0302e6b

Browse files
ANKUR DWIVEDIANKUR DWIVEDI
authored andcommitted
added pre and post transformation check
1 parent f5cb313 commit 0302e6b

File tree

4 files changed

+256
-33
lines changed

4 files changed

+256
-33
lines changed

src/constants/errorMessages.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,7 @@ export default {
1818
MISSING_SIGNATURE: { message: "Missing signature for upload. The SDK expects token, sginature and expire for authentication.", help: ""},
1919
MISSING_TOKEN: { message: "Missing token for upload. The SDK expects token, sginature and expire for authentication.", help: ""},
2020
MISSING_EXPIRE: { message: "Missing expire for upload. The SDK expects token, sginature and expire for authentication.", help: ""},
21+
INVALID_TRANSFORMATION: { message: "Invalid transformation parameter.", help: ""},
22+
INVALID_PRE_TRANSFORMATION: { message: "Invalid pre transformation parameter.", help: ""},
23+
INVALID_POST_TRANSFORMATION: { message: "Invalid post transformation parameter.", help: ""},
2124
};

src/interfaces/UploadOptions.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
1-
interface postTransformation{
2-
type: 'abs' | 'thumbnail' | 'transformation' | 'gif-to-video'
3-
protocol?: 'hls' | 'dash'
4-
value?: string
1+
interface TransformationObject {
2+
type: "transformation";
3+
value: string;
54
}
5+
6+
interface GifToVideoOrThumbnailObject {
7+
type: "gif-to-video" | "thumbnail";
8+
value?: string;
9+
}
10+
11+
interface AbsObject {
12+
type: "abs";
13+
value: string;
14+
protocol: "hls" | "dash";
15+
}
16+
17+
type postTransformation = TransformationObject | GifToVideoOrThumbnailObject | AbsObject;
18+
619
interface transformation{
720
pre?: string
821
post?: postTransformation[]

src/upload/index.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,33 @@ export const upload = (
3939
return
4040
}
4141

42+
if (uploadOptions.transformation) {
43+
if (!(Object.keys(uploadOptions.transformation).includes("pre") || Object.keys(uploadOptions.transformation).includes("post"))) {
44+
respond(true, errorMessages.INVALID_TRANSFORMATION, callback);
45+
return;
46+
}
47+
if (Object.keys(uploadOptions.transformation).includes("pre") && !uploadOptions.transformation.pre) {
48+
respond(true, errorMessages.INVALID_PRE_TRANSFORMATION, callback);
49+
return;
50+
}
51+
if (Object.keys(uploadOptions.transformation).includes("post")) {
52+
if (Array.isArray(uploadOptions.transformation.post)) {
53+
for (let transformation of uploadOptions.transformation.post) {
54+
if (transformation.type === "abs" && !(transformation.protocol || transformation.value)) {
55+
respond(true, errorMessages.INVALID_POST_TRANSFORMATION, callback);
56+
return;
57+
} else if (transformation.type === "transformation" && !transformation.value) {
58+
respond(true, errorMessages.INVALID_POST_TRANSFORMATION, callback);
59+
return;
60+
}
61+
}
62+
} else {
63+
respond(true, errorMessages.INVALID_POST_TRANSFORMATION, callback);
64+
return;
65+
}
66+
}
67+
}
68+
4269
var formData = new FormData();
4370
let key: keyof typeof uploadOptions;
4471
for (key in uploadOptions) {

test/upload.js

Lines changed: 209 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,34 +1092,214 @@ describe("File upload", function () {
10921092
sinon.assert.calledWith(callback, null, uploadSuccessResponseObj);
10931093
});
10941094

1095-
it('With pre and post transformation', async function () {
1096-
const fileOptions = {
1097-
...securityParameters,
1098-
fileName: "test_file_name",
1099-
file: "test_file",
1100-
responseFields: "tags, customCoordinates, isPrivateFile, metadata",
1101-
useUniqueFileName: false,
1102-
transformation: { pre: 'w-100', post: [{type: 'transformation', value: ''}]}
1103-
};
1104-
var callback = sinon.spy();
1105-
1106-
imagekit.upload(fileOptions, callback);
1107-
1108-
expect(server.requests.length).to.be.equal(1);
1109-
await sleep();
1110-
successUploadResponse();
1111-
await sleep();
1112-
1113-
var arg = server.requests[0].requestBody;
1114-
1115-
expect(arg.get('file')).to.be.equal("test_file");
1116-
expect(arg.get('fileName')).to.be.equal("test_file_name");
1117-
expect(arg.get('responseFields')).to.be.equal("tags, customCoordinates, isPrivateFile, metadata");
1118-
expect(arg.get('useUniqueFileName')).to.be.equal('false');
1119-
expect(arg.get('publicKey')).to.be.equal('test_public_key');
1120-
expect(arg.get('transformation')).to.be.equal(JSON.stringify(fileOptions.transformation));
1121-
1122-
expect(callback.calledOnce).to.be.true;
1123-
sinon.assert.calledWith(callback, null, uploadSuccessResponseObj);
1095+
it("With pre and post transformation", async function () {
1096+
const fileOptions = {
1097+
...securityParameters,
1098+
fileName: "test_file_name",
1099+
file: "test_file",
1100+
responseFields: "tags, customCoordinates, isPrivateFile, metadata",
1101+
useUniqueFileName: false,
1102+
transformation: { pre: "w-100", post: [{ type: "transformation", value: "w-100" }] },
1103+
};
1104+
var callback = sinon.spy();
1105+
1106+
imagekit.upload(fileOptions, callback);
1107+
1108+
expect(server.requests.length).to.be.equal(1);
1109+
await sleep();
1110+
successUploadResponse();
1111+
await sleep();
1112+
1113+
var arg = server.requests[0].requestBody;
1114+
1115+
expect(arg.get("file")).to.be.equal("test_file");
1116+
expect(arg.get("fileName")).to.be.equal("test_file_name");
1117+
expect(arg.get("responseFields")).to.be.equal("tags, customCoordinates, isPrivateFile, metadata");
1118+
expect(arg.get("useUniqueFileName")).to.be.equal("false");
1119+
expect(arg.get("publicKey")).to.be.equal("test_public_key");
1120+
expect(arg.get("transformation")).to.be.equal(JSON.stringify(fileOptions.transformation));
1121+
1122+
expect(callback.calledOnce).to.be.true;
1123+
sinon.assert.calledWith(callback, null, uploadSuccessResponseObj);
1124+
});
1125+
it("With pre transformation", async function () {
1126+
const fileOptions = {
1127+
...securityParameters,
1128+
fileName: "test_file_name",
1129+
file: "test_file",
1130+
responseFields: "tags, customCoordinates, isPrivateFile, metadata",
1131+
useUniqueFileName: false,
1132+
transformation: { pre: "w-100" },
1133+
};
1134+
var callback = sinon.spy();
1135+
1136+
imagekit.upload(fileOptions, callback);
1137+
1138+
expect(server.requests.length).to.be.equal(1);
1139+
await sleep();
1140+
successUploadResponse();
1141+
await sleep();
1142+
1143+
var arg = server.requests[0].requestBody;
1144+
1145+
expect(arg.get("file")).to.be.equal("test_file");
1146+
expect(arg.get("fileName")).to.be.equal("test_file_name");
1147+
expect(arg.get("responseFields")).to.be.equal("tags, customCoordinates, isPrivateFile, metadata");
1148+
expect(arg.get("useUniqueFileName")).to.be.equal("false");
1149+
expect(arg.get("publicKey")).to.be.equal("test_public_key");
1150+
expect(arg.get("transformation")).to.be.equal(JSON.stringify(fileOptions.transformation));
1151+
1152+
expect(callback.calledOnce).to.be.true;
1153+
sinon.assert.calledWith(callback, null, uploadSuccessResponseObj);
1154+
});
1155+
it("With post transformation", async function () {
1156+
const fileOptions = {
1157+
...securityParameters,
1158+
fileName: "test_file_name",
1159+
file: "test_file",
1160+
responseFields: "tags, customCoordinates, isPrivateFile, metadata",
1161+
useUniqueFileName: false,
1162+
transformation: { post: [{ type: "transformation", value: "w-100" }] },
1163+
};
1164+
var callback = sinon.spy();
1165+
1166+
imagekit.upload(fileOptions, callback);
1167+
1168+
expect(server.requests.length).to.be.equal(1);
1169+
await sleep();
1170+
successUploadResponse();
1171+
await sleep();
1172+
1173+
var arg = server.requests[0].requestBody;
1174+
1175+
expect(arg.get("file")).to.be.equal("test_file");
1176+
expect(arg.get("fileName")).to.be.equal("test_file_name");
1177+
expect(arg.get("responseFields")).to.be.equal("tags, customCoordinates, isPrivateFile, metadata");
1178+
expect(arg.get("useUniqueFileName")).to.be.equal("false");
1179+
expect(arg.get("publicKey")).to.be.equal("test_public_key");
1180+
expect(arg.get("transformation")).to.be.equal(JSON.stringify(fileOptions.transformation));
1181+
1182+
expect(callback.calledOnce).to.be.true;
1183+
sinon.assert.calledWith(callback, null, uploadSuccessResponseObj);
1184+
});
1185+
it("Should return error for invalid transformation", async function () {
1186+
const fileOptions = {
1187+
...securityParameters,
1188+
fileName: "test_file_name",
1189+
file: "test_file",
1190+
responseFields: "tags, customCoordinates, isPrivateFile, metadata",
1191+
useUniqueFileName: false,
1192+
transformation: {},
1193+
};
1194+
var callback = sinon.spy();
1195+
1196+
imagekit.upload(fileOptions, callback);
1197+
1198+
expect(server.requests.length).to.be.equal(1);
1199+
await sleep();
1200+
var errRes = {
1201+
help: "",
1202+
message: "Invalid transformation parameter.",
1203+
};
1204+
errorUploadResponse(500, errRes);
1205+
await sleep();
1206+
expect(callback.calledOnce).to.be.true;
1207+
sinon.assert.calledWith(callback, errRes, null);
1208+
});
1209+
it("Should return error for invalid pre transformation", async function () {
1210+
const fileOptions = {
1211+
...securityParameters,
1212+
fileName: "test_file_name",
1213+
file: "test_file",
1214+
responseFields: "tags, customCoordinates, isPrivateFile, metadata",
1215+
useUniqueFileName: false,
1216+
transformation: { pre: "" },
1217+
};
1218+
var callback = sinon.spy();
1219+
1220+
imagekit.upload(fileOptions, callback);
1221+
1222+
expect(server.requests.length).to.be.equal(1);
1223+
await sleep();
1224+
var errRes = {
1225+
help: "",
1226+
message: "Invalid pre transformation parameter.",
1227+
};
1228+
errorUploadResponse(500, errRes);
1229+
await sleep();
1230+
expect(callback.calledOnce).to.be.true;
1231+
sinon.assert.calledWith(callback, errRes, null);
1232+
});
1233+
it("Should return error for invalid post transformation of type abs", async function () {
1234+
const fileOptions = {
1235+
...securityParameters,
1236+
fileName: "test_file_name",
1237+
file: "test_file",
1238+
responseFields: "tags, customCoordinates, isPrivateFile, metadata",
1239+
useUniqueFileName: false,
1240+
transformation: { post: [{ type: "abs", value: "" }] },
1241+
};
1242+
var callback = sinon.spy();
1243+
1244+
imagekit.upload(fileOptions, callback);
1245+
1246+
expect(server.requests.length).to.be.equal(1);
1247+
await sleep();
1248+
var errRes = {
1249+
help: "",
1250+
message: "Invalid post transformation parameter.",
1251+
};
1252+
errorUploadResponse(500, errRes);
1253+
await sleep();
1254+
expect(callback.calledOnce).to.be.true;
1255+
sinon.assert.calledWith(callback, errRes, null);
1256+
});
1257+
it("Should return error for invalid post transformation of type transformation", async function () {
1258+
const fileOptions = {
1259+
...securityParameters,
1260+
fileName: "test_file_name",
1261+
file: "test_file",
1262+
responseFields: "tags, customCoordinates, isPrivateFile, metadata",
1263+
useUniqueFileName: false,
1264+
transformation: { post: [{ type: "transformation", value: "" }] },
1265+
};
1266+
var callback = sinon.spy();
1267+
1268+
imagekit.upload(fileOptions, callback);
1269+
1270+
expect(server.requests.length).to.be.equal(1);
1271+
await sleep();
1272+
var errRes = {
1273+
help: "",
1274+
message: "Invalid post transformation parameter.",
1275+
};
1276+
errorUploadResponse(500, errRes);
1277+
await sleep();
1278+
expect(callback.calledOnce).to.be.true;
1279+
sinon.assert.calledWith(callback, errRes, null);
1280+
});
1281+
it("Should return error for invalid post transformation if not an array", async function () {
1282+
const fileOptions = {
1283+
...securityParameters,
1284+
fileName: "test_file_name",
1285+
file: "test_file",
1286+
responseFields: "tags, customCoordinates, isPrivateFile, metadata",
1287+
useUniqueFileName: false,
1288+
transformation: { post: {} },
1289+
};
1290+
var callback = sinon.spy();
1291+
1292+
imagekit.upload(fileOptions, callback);
1293+
1294+
expect(server.requests.length).to.be.equal(1);
1295+
await sleep();
1296+
var errRes = {
1297+
help: "",
1298+
message: "Invalid post transformation parameter.",
1299+
};
1300+
errorUploadResponse(500, errRes);
1301+
await sleep();
1302+
expect(callback.calledOnce).to.be.true;
1303+
sinon.assert.calledWith(callback, errRes, null);
11241304
});
11251305
});

0 commit comments

Comments
 (0)