-
Notifications
You must be signed in to change notification settings - Fork 0
/
gridFs_mongoose.ts
62 lines (50 loc) · 1.72 KB
/
gridFs_mongoose.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import fs from "node:fs";
import mongoose from "mongoose";
async function start(URI:string) {
await mongoose.connect(URI);
const conn = mongoose.connection;
const Grid = new mongoose.mongo.GridFSBucket(conn.db, {
bucketName: "videos"
});
const readStream = fs.createReadStream("./assets/assests_18.mkv");
const uploadStream = Grid.openUploadStream("assests_18.mkv");
readStream.pipe(uploadStream);
readStream.on("error", () => {
console.log("Error in reading the file");
});
readStream.on("end", () => {
console.log("File uploaded successfully");
});
uploadStream.on("error", () => {
console.log("Error in uploading the file");
});
uploadStream.on("finish", () => {
console.log("File uploaded successfully");
});
}
async function getVideo(URI:string){
await mongoose.connect(URI);
const conn = mongoose.connection;
const Grid = new mongoose.mongo.GridFSBucket(conn.db, {
bucketName: "videos"
});
const downloadStream = Grid.openDownloadStream(new mongoose.Types.ObjectId("65bf6e059ce518911579ae02"), {
start: 0,
});
const writeStream = fs.createWriteStream("./output/assests_18.mkv");
downloadStream.pipe(writeStream);
downloadStream.on("error", () => {
console.log("Error in reading the file");
});
downloadStream.on("end", () => {
console.log("File downloaded successfully");
});
writeStream.on("error", () => {
console.log("Error in uploading the file");
});
writeStream.on("finish", () => {
console.log("File downloaded successfully");
});
return;
}
getVideo("mongodb+srv://naineel:[email protected]/GRID_FS?retryWrites=true&w=majority")