forked from dbroqua/MusicTopus
Version 1.1
Correction de bugs : * Avoir un logo pour les pages d'erreurs #32 * Stocker localement les assets d'un album #37 Co-authored-by: dbroqua <contact@darkou.fr> Reviewed-on: https://git.darkou.fr/dbroqua/MusicTopus/pulls/43
This commit is contained in:
parent
23c58459af
commit
6d0405d129
19 changed files with 1022 additions and 669 deletions
72
src/libs/aws.js
Normal file
72
src/libs/aws.js
Normal file
|
@ -0,0 +1,72 @@
|
|||
import AWS from "aws-sdk";
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import axios from "axios";
|
||||
import { v4 as uuid } from "uuid";
|
||||
|
||||
import {
|
||||
awsAccessKeyId,
|
||||
awsSecretAccessKey,
|
||||
s3BaseFolder,
|
||||
s3Endpoint,
|
||||
s3Bucket,
|
||||
s3Signature,
|
||||
} from "../config";
|
||||
|
||||
AWS.config.update({
|
||||
accessKeyId: awsAccessKeyId,
|
||||
secretAccessKey: awsSecretAccessKey,
|
||||
});
|
||||
/**
|
||||
* Fonction permettant de stocker un fichier local sur S3
|
||||
* @param {String} filename
|
||||
* @param {String} file
|
||||
* @param {Boolean} deleteFile
|
||||
*
|
||||
* @return {String}
|
||||
*/
|
||||
export const uploadFromFile = async (filename, file, deleteFile = false) => {
|
||||
const data = await fs.readFileSync(file);
|
||||
|
||||
const base64data = Buffer.from(data, "binary");
|
||||
const dest = path.join(s3BaseFolder, filename);
|
||||
|
||||
const s3 = new AWS.S3({
|
||||
endpoint: s3Endpoint,
|
||||
signatureVersion: s3Signature,
|
||||
});
|
||||
|
||||
await s3
|
||||
.putObject({
|
||||
Bucket: s3Bucket,
|
||||
Key: dest,
|
||||
Body: base64data,
|
||||
ACL: "public-read",
|
||||
})
|
||||
.promise();
|
||||
|
||||
if (deleteFile) {
|
||||
fs.unlinkSync(file);
|
||||
}
|
||||
|
||||
return `https://${s3Bucket}.${s3Endpoint}/${dest}`;
|
||||
};
|
||||
|
||||
/**
|
||||
* Fonction permettant de stocker un fichier provenant d'une URL sur S3
|
||||
* @param {String} url
|
||||
*
|
||||
* @return {String}
|
||||
*/
|
||||
export const uploadFromUrl = async (url) => {
|
||||
const filename = `${uuid()}.jpg`;
|
||||
const file = `/tmp/${filename}`;
|
||||
|
||||
const { data } = await axios.get(url, { responseType: "arraybuffer" });
|
||||
|
||||
fs.writeFileSync(file, data);
|
||||
|
||||
return uploadFromFile(filename, file, true);
|
||||
|
||||
// return s3Object;
|
||||
};
|
|
@ -1,11 +1,13 @@
|
|||
/* eslint-disable func-names */
|
||||
const mongoose = require("mongoose");
|
||||
const LocalStrategy = require("passport-local").Strategy;
|
||||
const { BasicStrategy } = require("passport-http");
|
||||
import { Strategy as LocalStrategy } from "passport-local";
|
||||
import { BasicStrategy } from "passport-http";
|
||||
import { Strategy as CustomStrategy } from "passport-custom";
|
||||
|
||||
const Users = mongoose.model("Users");
|
||||
import Users from "../models/users";
|
||||
|
||||
module.exports = function (passport) {
|
||||
import { jobsHeaderKey, jobsHeaderValue } from "../config";
|
||||
|
||||
export default (passport) => {
|
||||
passport.serializeUser((user, done) => {
|
||||
done(null, user);
|
||||
});
|
||||
|
@ -55,4 +57,17 @@ module.exports = function (passport) {
|
|||
.catch(done);
|
||||
})
|
||||
);
|
||||
passport.use(
|
||||
"jobs",
|
||||
new CustomStrategy((req, next) => {
|
||||
const apiKey = req.headers[jobsHeaderKey];
|
||||
|
||||
if (apiKey === jobsHeaderValue) {
|
||||
return next(null, {
|
||||
username: "jobs",
|
||||
});
|
||||
}
|
||||
return next(null, false, "Oops! Identifiants incorrects");
|
||||
})
|
||||
);
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue