mirror of
https://github.com/usatiuk/photos.git
synced 2025-10-28 15:27:49 +01:00
do not upload photos twice
This commit is contained in:
@@ -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"
|
||||
},
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user