Azure Blob integration
This commit is contained in:
28
services/renameBlob.js
Normal file
28
services/renameBlob.js
Normal file
@@ -0,0 +1,28 @@
|
||||
import azureBlob from "@azure/storage-blob";
|
||||
|
||||
const containerURL = `https://${process.env.AZURE_STORAGE_ACCOUNT_NAME}.blob.core.windows.net/img/`;
|
||||
const container = "img";
|
||||
const sharedKeyCredential = new azureBlob.StorageSharedKeyCredential(
|
||||
process.env.AZURE_STORAGE_ACCOUNT_NAME,
|
||||
process.env.AZURE_STORAGE_ACCOUNT_KEY
|
||||
);
|
||||
const pipeline = azureBlob.newPipeline(sharedKeyCredential);
|
||||
const blobServiceClient = new azureBlob.BlobServiceClient(
|
||||
`https://${process.env.AZURE_STORAGE_ACCOUNT_NAME}.blob.core.windows.net`,
|
||||
pipeline
|
||||
);
|
||||
|
||||
export default function renameBlob(blobURL) {
|
||||
const blobName = blobURL.replace(containerURL, "");
|
||||
const containerClient = blobServiceClient.getContainerClient(container);
|
||||
const tempBlob = containerClient.getBlobClient(blobName);
|
||||
const newBlob = containerClient.getBlobClient(removePrefix(blobName));
|
||||
|
||||
newBlob.syncCopyFromURL(tempBlob.url);
|
||||
return newBlob.url;
|
||||
}
|
||||
|
||||
function removePrefix(string) {
|
||||
const newString = string.replace("TEMP_", "");
|
||||
return newString;
|
||||
}
|
||||
@@ -1,11 +1,10 @@
|
||||
import Restaurant from "../models/restaurant.js";
|
||||
import express from "express";
|
||||
import Dish from "../models/dish.js";
|
||||
import User from "../models/users.js";
|
||||
import mongoose from "mongoose";
|
||||
import sanitizer from "string-sanitizer";
|
||||
import renameBlob from "./renameBlob.js";
|
||||
import jwt from "jsonwebtoken";
|
||||
import fs from "fs";
|
||||
import bcrypt from "bcrypt";
|
||||
import * as config from "../config/index.js";
|
||||
const { jwtSecret } = config;
|
||||
@@ -57,18 +56,14 @@ export function checkEmailTaken(email, callback) {
|
||||
});
|
||||
}
|
||||
|
||||
export function validateUserToken(token, callback) {
|
||||
jwt.verify(token, jwtSecret, { ignoreExpiration: false }, (err, decoded) => {
|
||||
if (err) {
|
||||
callback(false);
|
||||
} else {
|
||||
if (decoded === undefined) {
|
||||
callback(false);
|
||||
} else {
|
||||
callback(decoded);
|
||||
}
|
||||
}
|
||||
});
|
||||
export function validateUserToken(token) {
|
||||
let verified;
|
||||
try {
|
||||
verified = jwt.verify(token, jwtSecret, { ignoreExpiration: false });
|
||||
} catch (error) {
|
||||
verified = false;
|
||||
}
|
||||
return verified; // should be return verified for production
|
||||
}
|
||||
|
||||
export function validateDishId(id, callback) {
|
||||
@@ -132,33 +127,6 @@ export function createDish(dish, cookie, generateId) {
|
||||
}
|
||||
}
|
||||
|
||||
function renameImage(imagePath) {
|
||||
var newPath = imagePath.replace("_TEMP", "");
|
||||
fs.rename(imagePath, newPath, (err) => {
|
||||
if (err) console.log(err);
|
||||
});
|
||||
return newPath;
|
||||
}
|
||||
|
||||
function chooseImg(cookie, originalPath) {
|
||||
var cookiePath = decodeURI(cookie);
|
||||
if (cookiePath != originalPath) {
|
||||
return saveImage(cookie);
|
||||
} else {
|
||||
return originalPath;
|
||||
}
|
||||
}
|
||||
|
||||
export function saveImage(cookie) {
|
||||
if (cookie == undefined) {
|
||||
return undefined;
|
||||
} else {
|
||||
var decodedCookie = decodeURI(cookie);
|
||||
var newPath = renameImage(decodedCookie);
|
||||
return newPath;
|
||||
}
|
||||
}
|
||||
|
||||
export function yearFromNowDate() {
|
||||
Date.prototype.addDays = function (days) {
|
||||
var date = new Date(this.valueOf());
|
||||
@@ -242,3 +210,9 @@ function toShortDate(date) {
|
||||
|
||||
return shortDate;
|
||||
}
|
||||
|
||||
export function saveImage(url) {
|
||||
const newURL = renameBlob(url);
|
||||
|
||||
return newURL;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user