Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
PORT=3001
PORT=4000
MONGODB_LOCAL_URL=mongodb://localhost:27017/graphql-upload
BASE_URL = http://localhost:
23 changes: 15 additions & 8 deletions Middlewares/file.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
const {parse, join} = require("path"); // This is node built in package
const {createWriteStream} = require("fs"); // this is node built in package
const fs = require("fs"); // this is node built in package
const {parse, join} = require("path");
const {createWriteStream} = require("fs");
const fs = require("fs");

module.exports.readFile = async (file) => {
const {createReadStream, filename} = await file;
const stream = createReadStream();
var {ext, name} = parse(filename);
var {ext, name} = parse(filename); // extension and name separate
name = `single${Math.floor((Math.random() * 10000) + 1)}`;
let url = join(__dirname, `../Upload/${name}-${Date.now()}${ext}`);
let url = join(__dirname, `../Upload/${name}-${Date.now()}${ext}`); // directory url
const imageStream = await createWriteStream(url)
await stream.pipe(imageStream);
const baseUrl = process.env.BASE_URL
const port = process.env.PORT
url = `${baseUrl}${port}${url.split('Upload')[1]}`;
url = `${baseUrl}${port}${url.split('Upload')[1]}`; // server url
return url;
} // This is single readfile
}

module.exports.multipleReadFile = async (file) => {
let fileUrl = [];
Expand All @@ -32,4 +32,11 @@ module.exports.multipleReadFile = async (file) => {
fileUrl.push({url});
}
return fileUrl
}
}
// Quey for image upload in Altair
// mutation singleUpload($file:Upload!){
// singleUpload(file:$file){
// message
// }

// }
7 changes: 2 additions & 5 deletions Resolvers/uploadResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = {
Mutation: {
singleUpload: async (_, {file}) => {
const imageUrl = await readFile(file);
const singlefile = new SingleFile({image: imageUrl});
const singlefile = new SingleFile({image: imageUrl}); // saving image url in db
await singlefile.save();
return {
message: "Single file uploaded successfully!"
Expand All @@ -27,7 +27,4 @@ module.exports = {
}
}
}
} // That's it, Let's check it.
//Let's check it

// Let's create function for multiple File Upload
}
Binary file added Upload/img/single9368-1663320709927.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const express = require("express");
const cors = require("cors");

const app = express();
app.use(express.static('./Upload')) // fo image access using url

app.use(express.json());
app.use(cors());
Expand Down
Loading