Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Node Server can now accept base64 images with validation and will sav…
…e an image locally.
- Loading branch information
1 parent
f2c9c67
commit 3d796ea
Showing
11 changed files
with
133 additions
and
28 deletions.
There are no files selected for viewing
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* @Author: Pankaj Kumar. | ||
* @Date: 17 June 2019 | ||
* @Source : https://jsonworld.com/ | ||
* @Topic : base-64-image-upload | ||
*/ | ||
|
||
const express = require("express"); | ||
const app = express(); | ||
const port = 3000; | ||
const bodyParser = require("body-parser"); | ||
const fs = require("fs"); | ||
const mime = require("mime"); | ||
|
||
// parse application/x-www-form-urlencoded | ||
app.use(bodyParser.urlencoded({ extended: false })); | ||
|
||
// parse application/json | ||
app.use(bodyParser.json()); | ||
|
||
const uploadImage = async (req, res, next) => { | ||
// to declare some path to store your converted image | ||
var matches = req.body.base64image.match(/^data:([A-Za-z-+/]+);base64,(.+)$/), | ||
response = {}; | ||
|
||
if (matches.length !== 3) { | ||
return new Error("Invalid input string"); | ||
} | ||
|
||
response.type = matches[1]; | ||
response.data = new Buffer(matches[2], "base64"); | ||
let decodedImg = response; | ||
let imageBuffer = decodedImg.data; | ||
let type = decodedImg.type; | ||
let extension = mime.extension(type); | ||
let fileName = "image." + extension; | ||
try { | ||
fs.writeFileSync("./images/" + fileName, imageBuffer, "utf8"); | ||
return res.send({ status: "success" }); | ||
} catch (e) { | ||
next(e); | ||
} | ||
}; | ||
|
||
app.post("/upload/image", uploadImage); | ||
|
||
app.listen(port, () => console.log(`Server is listening on port ${port}`)); |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,10 @@ | ||
import {Request, Response} from "express"; | ||
import validateRequestIsImg from "../utils/validate-request-img"; | ||
|
||
const wrongContentErrorMsg = "Please send an image that is .jpg, .jpeg, .jpe or .png"; | ||
import { Request, Response } from "express"; | ||
import { prepareData } from "../utils/prepareData"; | ||
import { decodeAndStoreImg } from "../utils/decodeAndStoreImg"; | ||
|
||
export function imageResize (req: Request, res: Response) { | ||
const isValidRequestType = validateRequestIsImg(req); | ||
|
||
if(isValidRequestType) { | ||
|
||
} | ||
|
||
return res.status(400).send(wrongContentErrorMsg); | ||
const matches = prepareData(req, res); | ||
const isMatchesAnArray = Array.isArray(matches); | ||
if(isMatchesAnArray) return decodeAndStoreImg(matches, res); | ||
return matches; | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import mime from "mime"; | ||
import fs from "fs"; | ||
import { Response } from "express"; | ||
|
||
export function decodeAndStoreImg(matches: RegExpMatchArray, res: Response) { | ||
const response = { | ||
type: matches[1], | ||
data: Buffer.from(matches[2], "base64") | ||
} | ||
|
||
let decodedImg = response; | ||
let imageBuffer = decodedImg.data; | ||
let type = decodedImg.type; | ||
let extension = mime.extension(type); | ||
let fileName = `image-${Date.now()}.${extension}`; | ||
try { | ||
fs.writeFileSync(`images/${fileName}`, imageBuffer, "utf8"); | ||
return res.send({ status: "The image was successfully uploaded!" }); | ||
} catch (error) { | ||
throw (error); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export const errorMessages = { | ||
notAnJpegOrPng: "Please send an image that is .jpg, .jpeg, .jpe or .png", | ||
notBase64Structured: 'This is not a properly formatted base64 image' | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Request, Response } from "express"; | ||
import { errorMessages } from "../utils/errorMessages"; | ||
import validateRequestIsBase64Format from "../utils/validate-base64-request"; | ||
import validateRequestIsImg from "../utils/validate-request-is-img"; | ||
|
||
export function prepareData(req: Request,res: Response) { | ||
const isThereABase64StringVariable = 'base64String' in req.body; | ||
if(!isThereABase64StringVariable) return res.status(415).send(errorMessages.notBase64Structured); | ||
|
||
const { base64String } = req.body; | ||
const foundMatches = validateRequestIsBase64Format(base64String); | ||
if(!foundMatches) return res.status(400).send(errorMessages.notBase64Structured); | ||
|
||
const isCompatibleImage = validateRequestIsImg(foundMatches[1]); | ||
if(!isCompatibleImage) return res.status(415).send(errorMessages.notAnJpegOrPng); | ||
|
||
return foundMatches; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
export default function validateRequestIsBase64Format (base64String: string) { | ||
if (base64String === '' || base64String.trim() === '') return false; | ||
|
||
const base64StructureRegex = (/^data:([A-Za-z-+/]+);base64,(.+)$/); | ||
const isBase64StructuredString = base64StructureRegex.test(base64String); | ||
|
||
if(isBase64StructuredString) { | ||
const foundMatches = base64String.match(base64StructureRegex); | ||
if(foundMatches) { | ||
const isNotFormattedCorrectly = foundMatches.length !== 3; | ||
if(isNotFormattedCorrectly) return false; | ||
return foundMatches; | ||
} | ||
} | ||
|
||
return false; | ||
} |
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import validImageTypes from "./valid-img-types"; | ||
|
||
export default function validateRequestIsImg (dataType: string) { | ||
for(const [key, value] of Object.entries(validImageTypes)) { | ||
const isValidDataType = dataType === key; | ||
if(isValidDataType) return true; | ||
} | ||
|
||
return false; | ||
} |