do not upload photos twice

This commit is contained in:
2020-10-14 20:02:05 +03:00
committed by Stepan Usatiuk
parent ece1ffd105
commit 661d2ef0c7
3 changed files with 65 additions and 1 deletions

View File

@@ -3,7 +3,7 @@
"scripts": {
"start": "parcel serve src/index.html",
"build": "parcel build src/index.html",
"lint": "eslint ./src/** --ext .js,.jsx,.ts,.tsx && tsc --noEmit",
"lint": "eslint ./src/** --ext .js,.jsx,.ts,.tsx",
"lint-fix": "eslint ./src/** --ext .js,.jsx,.ts,.tsx --fix",
"test": "jest"
},

View File

@@ -69,6 +69,11 @@ photosRouter.post("/photos/upload/:id", async (ctx) => {
return;
}
if (await photo.isUploaded()) {
ctx.throw(400, "Already uploaded");
return;
}
if (ctx.request.files) {
const files = ctx.request.files;
if (Object.keys(files).length > 1) {

View File

@@ -140,6 +140,65 @@ describe("photos", function () {
);
});
it("should not upload a photo twice", async function () {
const response = await request(callback)
.post("/photos/new")
.set({
Authorization: `Bearer ${seed.user1.toJWT()}`,
"Content-Type": "application/json",
})
.send({
hash: dogHash,
size: dogSize,
format: dogFormat,
} as IPhotosNewPostBody)
.expect(200);
expect(response.body.error).to.be.false;
const photo = response.body.data as IPhotoJSON;
expect(photo.hash).to.be.equal(dogHash);
const dbPhoto = await Photo.findOneOrFail({
id: photo.id,
user: seed.user1.id as any,
});
expect(dbPhoto.hash).to.be.equal(dogHash);
expect(await dbPhoto.isUploaded()).to.be.equal(false);
await request(callback)
.post(`/photos/upload/${photo.id}`)
.set({
Authorization: `Bearer ${seed.user1.toJWT()}`,
"Content-Type": "application/json",
})
.attach("photo", dogPath)
.expect(200);
expect(await dbPhoto.isUploaded()).to.be.equal(true);
await request(callback)
.post(`/photos/upload/${photo.id}`)
.set({
Authorization: `Bearer ${seed.user1.toJWT()}`,
"Content-Type": "application/json",
})
.attach("photo", dogPath)
.expect(400);
const showResp = await request(callback)
.get(`/photos/showByID/${photo.id}`)
.set({
Authorization: `Bearer ${seed.user1.toJWT()}`,
})
.expect(200);
expect(parseInt(showResp.header["content-length"])).to.equal(
dogFileSize,
);
});
it("should not upload a wrong photo", async function () {
const response = await request(callback)
.post("/photos/new")