Last active
April 25, 2021 09:33
-
-
Save kuc-arc-f/2abe3dc7047c598db7a8231bb9cf3d2d to your computer and use it in GitHub Desktop.
image upload, save Webp format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// next.js image upload, save Webp format | |
// pages/api/file_upload.js | |
import multer from 'multer'; | |
import initMiddleware from '../../libs/init-middleware'; | |
const fs = require('fs'); | |
const path = require('path'); | |
const sharp = require('sharp'); | |
const upload = multer({ | |
storage: multer.diskStorage({ | |
destination: './public/uploads', | |
filename: (req, file, cb) => cb(null, file.originalname), | |
}), | |
}); | |
const multerAny = initMiddleware( | |
upload.any() | |
); | |
/* | |
type NextApiRequestWithFormData = NextApiRequest & { | |
files: any[], | |
} | |
*/ | |
export const config = { | |
api: { | |
bodyParser: false, | |
}, | |
} | |
const changeWebpImages = (imgPath, outputDir, outputFilePath) => { | |
const fileName = outputFilePath.split('/').reverse()[0]; // 拡張子を含む画像ファイル名 | |
const imgName = fileName.split('.')[0]; // 拡張子を除く画像ファイル名 | |
sharp(imgPath) | |
.webp({ | |
quality: 75 | |
}) | |
.toFile(`${outputDir}${imgName}.webp`, (err) => { | |
if ( err ) console.error(err); | |
return; | |
}); | |
}; | |
// | |
export default async function (req, res){ | |
try{ | |
await multerAny(req, res); | |
if (!req.files?.length || req.files.length > 1) { | |
res.statusCode = 400; | |
res.end(); | |
return; | |
} | |
//console.log("len=", req.files.length) | |
const blob= req.files[0]; | |
console.log("originalname=", blob.originalname) | |
var originalname = blob.originalname | |
var dir_base = "/home/naka/work/node/react/nextjs_3_12image/" | |
var dir_public = dir_base + "public/uploads/" | |
var new_name = "temp_" + originalname | |
fs.rename(dir_public+ originalname , dir_public + new_name, (err) => { | |
if (err) throw err; | |
}); | |
changeWebpImages(dir_public + new_name, dir_public, dir_public + new_name) | |
//console.log(dir_base) | |
res.json({}); | |
} catch (err) { | |
console.log(err); | |
res.status(500).send(); | |
} | |
}; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "nextjs_3_12image", | |
"version": "1.0.0", | |
"description": "hoge", | |
"main": "index.js", | |
"scripts": { | |
"dev": "next dev", | |
"build": "next build", | |
"start": "next start" | |
}, | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"bcrypt": "^5.0.0", | |
"cookie": "^0.4.1", | |
"csrf": "^3.1.0", | |
"mkdirp": "^1.0.4", | |
"moment": "^2.29.1", | |
"multer": "^1.4.2", | |
"next": "^10.1.3", | |
"next-cookies": "^2.0.3", | |
"next-flash": "^1.0.2", | |
"next-iron-session": "^4.1.12", | |
"react": "^17.0.2", | |
"react-dom": "^17.0.2", | |
"request-image-size": "^2.2.0", | |
"sharp": "^0.28.1" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment