my-typescript-library-rahul52us 2.4.9 → 2.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -40,7 +40,8 @@ const mongoose_1 = __importStar(require("mongoose"));
|
|
|
40
40
|
const stream_1 = require("stream");
|
|
41
41
|
const fileSystem_schema_1 = __importDefault(require("../repository/schemas/fileSystem.schema"));
|
|
42
42
|
const { GridFSBucket } = mongoose_1.mongo;
|
|
43
|
-
|
|
43
|
+
// Core upload logic wrapped as a helper
|
|
44
|
+
function tryUploadFile(file, name, type) {
|
|
44
45
|
return __awaiter(this, void 0, void 0, function* () {
|
|
45
46
|
return new Promise((resolve, reject) => {
|
|
46
47
|
try {
|
|
@@ -57,17 +58,11 @@ function uploadFile(file, name, type) {
|
|
|
57
58
|
uploadStream.on("finish", () => {
|
|
58
59
|
const fileId = uploadStream.id;
|
|
59
60
|
fileSystem_schema_1.default
|
|
60
|
-
.create({
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
.then((data) => {
|
|
64
|
-
resolve(data);
|
|
65
|
-
})
|
|
66
|
-
.catch((err) => {
|
|
67
|
-
reject("File upload failed");
|
|
68
|
-
});
|
|
61
|
+
.create({ fileId: fileId })
|
|
62
|
+
.then((data) => resolve(data))
|
|
63
|
+
.catch(() => reject("File upload failed"));
|
|
69
64
|
});
|
|
70
|
-
uploadStream.on("error", (
|
|
65
|
+
uploadStream.on("error", () => {
|
|
71
66
|
reject("Error uploading file");
|
|
72
67
|
});
|
|
73
68
|
}
|
|
@@ -77,6 +72,25 @@ function uploadFile(file, name, type) {
|
|
|
77
72
|
});
|
|
78
73
|
});
|
|
79
74
|
}
|
|
75
|
+
// Public function with retry logic
|
|
76
|
+
function uploadFile(file, name, type) {
|
|
77
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
78
|
+
const maxRetries = 3;
|
|
79
|
+
let attempt = 0;
|
|
80
|
+
while (attempt < maxRetries) {
|
|
81
|
+
try {
|
|
82
|
+
return yield tryUploadFile(file, name, type); // success → exit
|
|
83
|
+
}
|
|
84
|
+
catch (error) {
|
|
85
|
+
attempt++;
|
|
86
|
+
if (attempt >= maxRetries) {
|
|
87
|
+
throw new Error(`File upload failed after ${maxRetries} attempts`);
|
|
88
|
+
}
|
|
89
|
+
yield new Promise((res) => setTimeout(res, 300 * attempt)); // wait before retry
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
}
|
|
80
94
|
exports.uploadFile = uploadFile;
|
|
81
95
|
function getFile(id) {
|
|
82
96
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -91,13 +105,11 @@ function getFile(id) {
|
|
|
91
105
|
chunks.push(chunk);
|
|
92
106
|
fileSize += chunk.length;
|
|
93
107
|
});
|
|
94
|
-
downloadStream.on("error", (
|
|
108
|
+
downloadStream.on("error", () => {
|
|
95
109
|
reject("An error occured while fetching file");
|
|
96
110
|
});
|
|
97
111
|
downloadStream.on("end", () => {
|
|
98
112
|
const buffer = Buffer.concat(chunks, fileSize);
|
|
99
|
-
// Use the `buffer` variable as needed, e.g., save it to a file or process it further
|
|
100
|
-
// For example, you can assign it to a variable:
|
|
101
113
|
const downloadData = buffer;
|
|
102
114
|
const array = new Uint8Array(downloadData);
|
|
103
115
|
resolve(`[${array}]`);
|
|
@@ -119,13 +131,12 @@ function getFileData(id) {
|
|
|
119
131
|
const downloadStream = bucket.openDownloadStream(new mongoose_1.default.Types.ObjectId(id));
|
|
120
132
|
const chunks = [];
|
|
121
133
|
let fileSize = 0;
|
|
122
|
-
|
|
123
|
-
|
|
134
|
+
connection.db
|
|
135
|
+
.collection("fs.files")
|
|
136
|
+
.findOne({ _id: new mongoose_1.default.Types.ObjectId(id) })
|
|
124
137
|
.then((fileInfo) => {
|
|
125
|
-
if (!fileInfo)
|
|
138
|
+
if (!fileInfo)
|
|
126
139
|
return reject("File not found");
|
|
127
|
-
}
|
|
128
|
-
// Push metadata for the response
|
|
129
140
|
const { filename, contentType, length } = fileInfo;
|
|
130
141
|
downloadStream.on("data", (chunk) => {
|
|
131
142
|
chunks.push(chunk);
|
|
@@ -138,13 +149,12 @@ function getFileData(id) {
|
|
|
138
149
|
const buffer = Buffer.concat(chunks, fileSize);
|
|
139
150
|
const downloadData = buffer;
|
|
140
151
|
const array = new Uint8Array(downloadData);
|
|
141
|
-
// Return an object containing file metadata and data
|
|
142
152
|
resolve({
|
|
143
153
|
fileId: id,
|
|
144
|
-
filename
|
|
145
|
-
contentType
|
|
154
|
+
filename,
|
|
155
|
+
contentType,
|
|
146
156
|
fileSize: length,
|
|
147
|
-
data: array,
|
|
157
|
+
data: array,
|
|
148
158
|
});
|
|
149
159
|
});
|
|
150
160
|
})
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"filesystem.repository.js","sourceRoot":"","sources":["../../src/repository/filesystem.repository.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,
|
|
1
|
+
{"version":3,"file":"filesystem.repository.js","sourceRoot":"","sources":["../../src/repository/filesystem.repository.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,qDAA2C;AAC3C,mCAAkC;AAElC,gGAAuE;AACvE,MAAM,EAAE,YAAY,EAAE,GAAG,gBAAK,CAAC;AAE/B,wCAAwC;AACxC,SAAe,aAAa,CAAC,IAAS,EAAE,IAAY,EAAE,IAAY;;QAChE,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YACrC,IAAI;gBACF,MAAM,UAAU,GAAG,kBAAQ,CAAC,UAAU,CAAC;gBACvC,MAAM,MAAM,GAAG,IAAI,YAAY,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC;gBAE/C,MAAM,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;gBAC3C,MAAM,cAAc,GAAG,IAAI,iBAAQ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AAC,EAAE,CAAC,OAAO,EAAE,GAAG,EAAE;wBAC9B,MAAM,CAAC,2CAA2C,CAAC,CAAC;oBACtD,CAAC,CAAC,CAAC;oBAEH,cAAc,CAAC,EAAE,CAAC,KAAK,EAAE,GAAG,EAAE;wBAC5B,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;wBAC/C,MAAM,YAAY,GAAG,MAAM,CAAC;wBAC5B,MAAM,KAAK,GAAG,IAAI,UAAU,CAAC,YAAY,CAAC,CAAC;wBAE3C,OAAO,CAAC;4BACN,MAAM,EAAE,EAAE;4BACV,QAAQ;4BACR,WAAW;4BACX,QAAQ,EAAE,MAAM;4BAChB,IAAI,EAAE,KAAK;yBACZ,CAAC,CAAC;oBACL,CAAC,CAAC,CAAC;gBACL,CAAC,CAAC;qBACD,KAAK,CAAC,GAAG,EAAE;oBACV,MAAM,CAAC,8BAA8B,CAAC,CAAC;gBACzC,CAAC,CAAC,CAAC;aACN;YAAC,OAAO,GAAG,EAAE;gBACZ,MAAM,CAAC,wBAAwB,CAAC,CAAC;aAClC;QACH,CAAC,CAAC,CAAC;IACL,CAAC;CAAA;AAhDD,kCAgDC"}
|
package/package.json
CHANGED
|
@@ -1,134 +1,138 @@
|
|
|
1
|
-
import mongoose, {
|
|
1
|
+
import mongoose, { mongo } from "mongoose";
|
|
2
2
|
import { Readable } from "stream";
|
|
3
3
|
|
|
4
4
|
import fileSystemSchema from "../repository/schemas/fileSystem.schema";
|
|
5
5
|
const { GridFSBucket } = mongo;
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
// Core upload logic wrapped as a helper
|
|
8
|
+
async function tryUploadFile(file: any, name: string, type: string) {
|
|
8
9
|
return new Promise((resolve, reject) => {
|
|
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
|
-
|
|
10
|
+
try {
|
|
11
|
+
const connection = mongoose.connection;
|
|
12
|
+
const bucket = new GridFSBucket(connection.db);
|
|
13
|
+
|
|
14
|
+
const buffer = Buffer.from(file, 'base64');
|
|
15
|
+
const readableStream = new Readable();
|
|
16
|
+
readableStream.push(buffer);
|
|
17
|
+
readableStream.push(null);
|
|
18
|
+
|
|
19
|
+
const uploadStream = bucket.openUploadStream(name, {
|
|
20
|
+
metadata: { contentType: type },
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
readableStream.pipe(uploadStream);
|
|
24
|
+
|
|
25
|
+
uploadStream.on("finish", () => {
|
|
26
|
+
const fileId = uploadStream.id;
|
|
27
|
+
fileSystemSchema
|
|
28
|
+
.create({ fileId: fileId })
|
|
29
|
+
.then((data) => resolve(data))
|
|
30
|
+
.catch(() => reject("File upload failed"));
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
uploadStream.on("error", () => {
|
|
34
|
+
reject("Error uploading file");
|
|
35
|
+
});
|
|
36
|
+
} catch (err) {
|
|
37
|
+
reject("Error uploading the file");
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
}
|
|
37
41
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
42
|
+
// Public function with retry logic
|
|
43
|
+
export async function uploadFile(file: any, name: string, type: string) {
|
|
44
|
+
const maxRetries = 3;
|
|
45
|
+
let attempt = 0;
|
|
46
|
+
while (attempt < maxRetries) {
|
|
47
|
+
try {
|
|
48
|
+
return await tryUploadFile(file, name, type); // success → exit
|
|
49
|
+
} catch (error) {
|
|
50
|
+
attempt++;
|
|
51
|
+
if (attempt >= maxRetries) {
|
|
52
|
+
throw new Error(`File upload failed after ${maxRetries} attempts`);
|
|
43
53
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
54
|
+
await new Promise((res) => setTimeout(res, 300 * attempt)); // wait before retry
|
|
55
|
+
}
|
|
56
|
+
}
|
|
47
57
|
}
|
|
48
58
|
|
|
49
59
|
export async function getFile(id: string) {
|
|
50
60
|
return new Promise((resolve, reject) => {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
61
|
+
try {
|
|
62
|
+
const connection = mongoose.connection;
|
|
63
|
+
const bucket = new GridFSBucket(connection.db);
|
|
64
|
+
const downloadStream = bucket.openDownloadStream(new mongoose.Types.ObjectId(id));
|
|
65
|
+
|
|
66
|
+
const chunks: Buffer[] = [];
|
|
67
|
+
let fileSize = 0;
|
|
68
|
+
|
|
69
|
+
downloadStream.on("data", (chunk) => {
|
|
70
|
+
chunks.push(chunk);
|
|
71
|
+
fileSize += chunk.length;
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
downloadStream.on("error", () => {
|
|
75
|
+
reject("An error occured while fetching file");
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
downloadStream.on("end", () => {
|
|
79
|
+
const buffer = Buffer.concat(chunks, fileSize);
|
|
80
|
+
const downloadData = buffer;
|
|
81
|
+
const array = new Uint8Array(downloadData);
|
|
82
|
+
resolve(`[${array}]`);
|
|
83
|
+
});
|
|
84
|
+
} catch (err) {
|
|
85
|
+
reject("Error Getting the file");
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export async function getFileData(id: string) {
|
|
91
|
+
return new Promise((resolve, reject) => {
|
|
92
|
+
try {
|
|
93
|
+
const connection = mongoose.connection;
|
|
94
|
+
const bucket = new GridFSBucket(connection.db);
|
|
95
|
+
const downloadStream = bucket.openDownloadStream(new mongoose.Types.ObjectId(id));
|
|
96
|
+
|
|
97
|
+
const chunks: Buffer[] = [];
|
|
98
|
+
let fileSize = 0;
|
|
99
|
+
|
|
100
|
+
connection.db
|
|
101
|
+
.collection("fs.files")
|
|
102
|
+
.findOne({ _id: new mongoose.Types.ObjectId(id) })
|
|
103
|
+
.then((fileInfo) => {
|
|
104
|
+
if (!fileInfo) return reject("File not found");
|
|
55
105
|
|
|
56
|
-
const
|
|
57
|
-
let fileSize = 0;
|
|
106
|
+
const { filename, contentType, length } = fileInfo;
|
|
58
107
|
|
|
59
108
|
downloadStream.on("data", (chunk) => {
|
|
60
109
|
chunks.push(chunk);
|
|
61
110
|
fileSize += chunk.length;
|
|
62
111
|
});
|
|
63
112
|
|
|
64
|
-
downloadStream.on("error", (
|
|
65
|
-
reject("An error
|
|
113
|
+
downloadStream.on("error", () => {
|
|
114
|
+
reject("An error occurred while fetching the file");
|
|
66
115
|
});
|
|
67
116
|
|
|
68
117
|
downloadStream.on("end", () => {
|
|
69
118
|
const buffer = Buffer.concat(chunks, fileSize);
|
|
70
|
-
|
|
71
|
-
// Use the `buffer` variable as needed, e.g., save it to a file or process it further
|
|
72
|
-
// For example, you can assign it to a variable:
|
|
73
119
|
const downloadData = buffer;
|
|
74
|
-
const array = new Uint8Array(downloadData)
|
|
75
|
-
|
|
120
|
+
const array = new Uint8Array(downloadData);
|
|
121
|
+
|
|
122
|
+
resolve({
|
|
123
|
+
fileId: id,
|
|
124
|
+
filename,
|
|
125
|
+
contentType,
|
|
126
|
+
fileSize: length,
|
|
127
|
+
data: array,
|
|
128
|
+
});
|
|
76
129
|
});
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
130
|
+
})
|
|
131
|
+
.catch(() => {
|
|
132
|
+
reject("Error fetching file metadata");
|
|
133
|
+
});
|
|
134
|
+
} catch (err) {
|
|
135
|
+
reject("Error Getting the file");
|
|
136
|
+
}
|
|
137
|
+
});
|
|
81
138
|
}
|
|
82
|
-
|
|
83
|
-
export async function getFileData(id: string) {
|
|
84
|
-
return new Promise((resolve, reject) => {
|
|
85
|
-
try {
|
|
86
|
-
const connection = mongoose.connection;
|
|
87
|
-
const bucket = new GridFSBucket(connection.db);
|
|
88
|
-
const downloadStream = bucket.openDownloadStream(new mongoose.Types.ObjectId(id));
|
|
89
|
-
|
|
90
|
-
const chunks: Buffer[] = [];
|
|
91
|
-
let fileSize = 0;
|
|
92
|
-
|
|
93
|
-
// Fetch the file's metadata from fs.files
|
|
94
|
-
connection.db.collection('fs.files').findOne({ _id: new mongoose.Types.ObjectId(id) })
|
|
95
|
-
.then((fileInfo) => {
|
|
96
|
-
if (!fileInfo) {
|
|
97
|
-
return reject("File not found");
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
// Push metadata for the response
|
|
101
|
-
const { filename, contentType, length } = fileInfo;
|
|
102
|
-
|
|
103
|
-
downloadStream.on("data", (chunk) => {
|
|
104
|
-
chunks.push(chunk);
|
|
105
|
-
fileSize += chunk.length;
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
downloadStream.on("error", () => {
|
|
109
|
-
reject("An error occurred while fetching the file");
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
downloadStream.on("end", () => {
|
|
113
|
-
const buffer = Buffer.concat(chunks, fileSize);
|
|
114
|
-
const downloadData = buffer;
|
|
115
|
-
const array = new Uint8Array(downloadData);
|
|
116
|
-
|
|
117
|
-
// Return an object containing file metadata and data
|
|
118
|
-
resolve({
|
|
119
|
-
fileId: id, // Include file ID
|
|
120
|
-
filename: filename,
|
|
121
|
-
contentType: contentType,
|
|
122
|
-
fileSize: length,
|
|
123
|
-
data: array, // Returning as Uint8Array
|
|
124
|
-
});
|
|
125
|
-
});
|
|
126
|
-
})
|
|
127
|
-
.catch(() => {
|
|
128
|
-
reject("Error fetching file metadata");
|
|
129
|
-
});
|
|
130
|
-
} catch (err) {
|
|
131
|
-
reject("Error Getting the file");
|
|
132
|
-
}
|
|
133
|
-
});
|
|
134
|
-
}
|