From 22933ad6b93bc724865d4d232b9d8e8d45b32af2 Mon Sep 17 00:00:00 2001 From: Zutatensuppe Date: Sun, 20 Jun 2021 14:07:35 +0200 Subject: [PATCH] add image sizes to db --- src/dbpatches/02_image_sizes.sqlite | 22 ++++++++++++++++++++++ src/server/main.ts | 5 +++++ 2 files changed, 27 insertions(+) create mode 100644 src/dbpatches/02_image_sizes.sqlite diff --git a/src/dbpatches/02_image_sizes.sqlite b/src/dbpatches/02_image_sizes.sqlite new file mode 100644 index 0000000..35e0f39 --- /dev/null +++ b/src/dbpatches/02_image_sizes.sqlite @@ -0,0 +1,22 @@ +-- Add width/height to images table + +CREATE TABLE images_new ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + + created TIMESTAMP NOT NULL, + + filename TEXT NOT NULL UNIQUE, + filename_original TEXT NOT NULL, + title TEXT NOT NULL, + + width INTEGER NOT NULL, + height INTEGER NOT NULL +); + +INSERT INTO images_new +SELECT id, created, filename, filename_original, title, 0, 0 +FROM images; + +DROP TABLE images; + +ALTER TABLE images_new RENAME TO images; diff --git a/src/server/main.ts b/src/server/main.ts index 4d8e185..db403c5 100644 --- a/src/server/main.ts +++ b/src/server/main.ts @@ -176,11 +176,16 @@ app.post('/api/upload', (req, res): void => { res.status(400).send("Something went wrong!"); } + const dim = await Images.getDimensions( + `${UPLOAD_DIR}/${req.file.filename}` + ) const imageId = db.insert('images', { filename: req.file.filename, filename_original: req.file.originalname, title: req.body.title || '', created: Time.timestamp(), + width: dim.w, + height: dim.h, }) if (req.body.tags) {