-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathipfsQueue.js
More file actions
252 lines (213 loc) · 6.88 KB
/
ipfsQueue.js
File metadata and controls
252 lines (213 loc) · 6.88 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
const fs = require('fs-extra');
const IpfsOnlyHash = require('ipfs-only-hash');
const { EventEmitter } = require('events');
// Shared upload queue
const uploadQueue = [];
const uploadInProgress = new Map();
const queueEvents = new EventEmitter();
// Maximum concurrent uploads
const MAX_CONCURRENT_UPLOADS = 3;
// Queue states
const QUEUE_STATES = {
PENDING: 'pending',
VERIFYING: 'verifying',
UPLOADING: 'uploading',
COMPLETED: 'completed',
FAILED: 'failed'
};
// Add a file to the upload queue
function addToQueue(contractId, cid, filePath, expectedCid) {
const queueItem = {
contractId,
cid,
filePath,
expectedCid,
state: QUEUE_STATES.PENDING,
addedAt: Date.now(),
attempts: 0,
maxAttempts: 5
};
uploadQueue.push(queueItem);
queueEvents.emit('itemAdded', queueItem);
processQueue();
return queueItem;
}
// Process the upload queue
async function processQueue() {
// Check if we can process more items
if (uploadInProgress.size >= MAX_CONCURRENT_UPLOADS) {
return;
}
// Find next pending item
const pendingItem = uploadQueue.find(item =>
item.state === QUEUE_STATES.PENDING &&
!uploadInProgress.has(item.cid)
);
if (!pendingItem) {
return;
}
// Mark as in progress
uploadInProgress.set(pendingItem.cid, pendingItem);
pendingItem.state = QUEUE_STATES.VERIFYING;
try {
// Verify CID before uploading
const fileBuffer = await fs.readFile(pendingItem.filePath);
const calculatedCid = await IpfsOnlyHash.of(fileBuffer);
if (calculatedCid !== pendingItem.expectedCid) {
throw new Error(`CID mismatch: expected ${pendingItem.expectedCid}, got ${calculatedCid}`);
}
// CID verified, now upload to IPFS
pendingItem.state = QUEUE_STATES.UPLOADING;
await uploadToIPFS(pendingItem, fileBuffer);
// Mark as completed
pendingItem.state = QUEUE_STATES.COMPLETED;
queueEvents.emit('uploadCompleted', pendingItem);
} catch (error) {
pendingItem.attempts++;
console.error(`Upload failed for ${pendingItem.cid}:`, error.message);
if (pendingItem.attempts >= pendingItem.maxAttempts) {
pendingItem.state = QUEUE_STATES.FAILED;
pendingItem.error = error.message;
queueEvents.emit('uploadFailed', pendingItem);
} else {
// Retry after delay
pendingItem.state = QUEUE_STATES.PENDING;
setTimeout(() => processQueue(), Math.min(1000 * Math.pow(2, pendingItem.attempts), 30000));
}
} finally {
uploadInProgress.delete(pendingItem.cid);
// Process next item
setImmediate(processQueue);
}
}
// Upload file to IPFS with retry logic
async function uploadToIPFS(queueItem, fileBuffer) {
// Get ipfs instance from centralized module
const { getIPFSInstance } = require('./ipfsDirectClient');
const ipfs = getIPFSInstance();
if (!ipfs) {
throw new Error('IPFS client not initialized');
}
try {
// Upload file using new API
const result = await ipfs.add(fileBuffer);
// Verify the uploaded CID matches
if (result.cid.toString() !== queueItem.expectedCid) {
throw new Error(`IPFS returned different CID: ${result.cid.toString()}`);
}
// Pin the file
try {
await ipfs.pin.add(queueItem.expectedCid);
} catch (pinErr) {
console.error(`Failed to pin ${queueItem.expectedCid}:`, pinErr);
// Don't fail the upload if pinning fails
}
queueItem.ipfsResult = {
hash: result.cid.toString(),
path: result.path,
size: result.size
};
return queueItem.ipfsResult;
} catch (err) {
throw err;
}
}
// Get queue status
function getQueueStatus() {
const status = {
total: uploadQueue.length,
pending: uploadQueue.filter(item => item.state === QUEUE_STATES.PENDING).length,
verifying: uploadQueue.filter(item => item.state === QUEUE_STATES.VERIFYING).length,
uploading: uploadQueue.filter(item => item.state === QUEUE_STATES.UPLOADING).length,
completed: uploadQueue.filter(item => item.state === QUEUE_STATES.COMPLETED).length,
failed: uploadQueue.filter(item => item.state === QUEUE_STATES.FAILED).length,
inProgress: uploadInProgress.size
};
return status;
}
// Get items for a specific contract
function getContractItems(contractId) {
return uploadQueue.filter(item => item.contractId === contractId);
}
// Clean up completed items older than specified age
function cleanupQueue(maxAgeMs = 3600000) { // Default 1 hour
const cutoffTime = Date.now() - maxAgeMs;
const itemsToRemove = uploadQueue.filter(item =>
(item.state === QUEUE_STATES.COMPLETED || item.state === QUEUE_STATES.FAILED) &&
item.addedAt < cutoffTime
);
itemsToRemove.forEach(item => {
const index = uploadQueue.indexOf(item);
if (index > -1) {
uploadQueue.splice(index, 1);
}
});
return itemsToRemove.length;
}
// Clean up upload files
async function cleanupUploadFiles(daysOld = 7) {
const uploadsDir = './uploads';
const cutoffTime = Date.now() - (daysOld * 24 * 60 * 60 * 1000);
let cleanedCount = 0;
try {
const files = await fs.readdir(uploadsDir);
for (const file of files) {
const filePath = `${uploadsDir}/${file}`;
const stats = await fs.stat(filePath);
// Check if file is old enough and not in active queue
if (stats.mtimeMs < cutoffTime) {
const cid = file.split('-')[0];
const activeItem = uploadQueue.find(item =>
item.cid === cid &&
(item.state === QUEUE_STATES.PENDING ||
item.state === QUEUE_STATES.VERIFYING ||
item.state === QUEUE_STATES.UPLOADING)
);
if (!activeItem) {
await fs.remove(filePath);
cleanedCount++;
}
}
}
} catch (error) {
console.error('Error cleaning upload files:', error);
}
return cleanedCount;
}
// Verify CID without uploading
async function verifyCID(filePath, expectedCid) {
try {
const fileBuffer = await fs.readFile(filePath);
const calculatedCid = await IpfsOnlyHash.of(fileBuffer);
if(calculatedCid != expectedCid)console.log('calculatedCid', calculatedCid, 'expectedCid', expectedCid)
return calculatedCid === expectedCid;
} catch (error) {
console.error('Error verifying CID:', error);
return false;
}
}
// Start periodic cleanup
setInterval(() => {
const removedFromQueue = cleanupQueue();
if (removedFromQueue > 0) {
console.log(`Cleaned up ${removedFromQueue} completed/failed items from queue`);
}
}, 300000); // Every 5 minutes
// Daily cleanup of old upload files
setInterval(async () => {
const cleanedFiles = await cleanupUploadFiles();
if (cleanedFiles > 0) {
console.log(`Cleaned up ${cleanedFiles} old upload files`);
}
}, 86400000); // Every 24 hours
module.exports = {
addToQueue,
processQueue,
getQueueStatus,
getContractItems,
cleanupQueue,
cleanupUploadFiles,
verifyCID,
queueEvents,
QUEUE_STATES
};