fix png upload 500

This commit is contained in:
2020-10-16 13:45:50 +00:00
committed by Stepan Usatiuk
parent 0dcb5e2340
commit c878d08613
4 changed files with 76 additions and 0 deletions

View File

@@ -18,6 +18,11 @@ import {
dogPath,
dogSize,
ISeed,
pngFileSize,
pngFormat,
pngHash,
pngPath,
pngSize,
prepareMetadata,
seedDB,
} from "./util";
@@ -236,6 +241,66 @@ describe("photos", function () {
);
});
it("should create, upload and show a png file", async function () {
const response = await request(callback)
.post("/photos/new")
.set({
Authorization: `Bearer ${seed.user1.toJWT()}`,
"Content-Type": "application/json",
})
.send({
hash: pngHash,
size: pngSize,
format: pngFormat,
} as IPhotosNewPostBody)
.expect(200);
expect(response.body.error).to.be.false;
const photo = response.body.data as IPhotoReqJSON;
expect(photo.hash).to.be.equal(pngHash);
const dbPhoto = await Photo.findOneOrFail({
id: photo.id,
user: seed.user1.id as any,
});
expect(dbPhoto.hash).to.be.equal(pngHash);
expect(await dbPhoto.fileExists()).to.be.equal(false);
await request(callback)
.post(`/photos/upload/${photo.id}`)
.set({
Authorization: `Bearer ${seed.user1.toJWT()}`,
"Content-Type": "application/json",
})
.attach("photo", pngPath)
.expect(200);
const dbPhotoUpl = await Photo.findOneOrFail({
id: photo.id,
user: seed.user1.id as any,
});
expect(dbPhotoUpl.hash).to.be.equal(pngHash);
expect(dbPhotoUpl.format).to.be.equal(pngFormat);
expect(await dbPhotoUpl.fileExists()).to.be.equal(true);
expect(dbPhotoUpl.shotAt.getTime()).to.be.approximately(
new Date().getTime(),
10000,
);
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(
pngFileSize,
);
});
it("should not create a photo twice", async function () {
const response = await request(callback)
.post("/photos/new")

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 MiB

View File

@@ -6,6 +6,7 @@ import { getHash, getSize } from "~util";
export const dogPath = "./src/tests/integration/photos/dog.jpg";
export const catPath = "./src/tests/integration/photos/cat.jpg";
export const pngPath = "./src/tests/integration/photos/ee.png";
export interface ISeed {
user1: User;
@@ -22,6 +23,10 @@ export let catHash = "";
export let catSize = "";
export let catFileSize = 0;
export const catFormat = "image/jpeg";
export let pngHash = "";
export let pngSize = "";
export let pngFileSize = 0;
export const pngFormat = "image/png";
export async function prepareMetadata(): Promise<void> {
dogHash = await getHash(dogPath);
@@ -30,6 +35,9 @@ export async function prepareMetadata(): Promise<void> {
catHash = await getHash(catPath);
catSize = await getSize(catPath);
catFileSize = (await fs.stat(catPath)).size;
pngHash = await getHash(pngPath);
pngSize = await getSize(pngPath);
pngFileSize = (await fs.stat(pngPath)).size;
}
export async function seedDB(): Promise<ISeed> {

View File

@@ -22,6 +22,9 @@ export async function getSize(file: string): Promise<string> {
export async function getShotDate(file: string): Promise<Date | null> {
const tags = ExifReader.load(await fs.readFile(file));
if (!tags || !tags["DateTimeOriginal"]) {
return null;
}
const imageDate = tags["DateTimeOriginal"].description;
if (!imageDate) {
return null;