mirror of
				https://github.com/NodeBB/NodeBB.git
				synced 2025-10-30 18:46:01 +01:00 
			
		
		
		
	refactor: fix user uploads paths, and associate uid with user uploads
This commit is contained in:
		
							
								
								
									
										49
									
								
								src/upgrades/1.19.3/fix_user_uploads_zset.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										49
									
								
								src/upgrades/1.19.3/fix_user_uploads_zset.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,49 @@ | |||||||
|  | 'use strict'; | ||||||
|  |  | ||||||
|  | const crypto = require('crypto'); | ||||||
|  |  | ||||||
|  | const db = require('../../database'); | ||||||
|  | const batch = require('../../batch'); | ||||||
|  | const user = require('../../user'); | ||||||
|  |  | ||||||
|  | const md5 = filename => crypto.createHash('md5').update(filename).digest('hex'); | ||||||
|  |  | ||||||
|  | module.exports = { | ||||||
|  | 	name: 'Fix paths in user uploads sorted sets', | ||||||
|  | 	timestamp: Date.UTC(2022, 1, 10), | ||||||
|  | 	method: async function () { | ||||||
|  | 		const { progress } = this; | ||||||
|  |  | ||||||
|  | 		await batch.processSortedSet('users:joindate', async (uids) => { | ||||||
|  | 			let keys = uids.map(uid => `uid:${uid}:uploads`); | ||||||
|  | 			const exists = await db.exists(keys); | ||||||
|  | 			keys = keys.filter((key, idx) => exists[idx]); | ||||||
|  |  | ||||||
|  | 			progress.incr(uids.length - keys.length); | ||||||
|  |  | ||||||
|  | 			await Promise.all(keys.map(async (key, idx) => { | ||||||
|  | 				// Rename the paths within | ||||||
|  | 				let uploads = await db.getSortedSetRangeWithScores(key, 0, -1); | ||||||
|  |  | ||||||
|  | 				// Don't process those that have already the right format | ||||||
|  | 				uploads = uploads.filter(upload => upload.value.startsWith('/files/')); | ||||||
|  |  | ||||||
|  | 				await db.sortedSetRemove(key, uploads.map(upload => upload.value)); | ||||||
|  | 				await db.sortedSetAdd( | ||||||
|  | 					key, | ||||||
|  | 					uploads.map(upload => upload.score), | ||||||
|  | 					uploads.map(upload => upload.value.slice(1)) | ||||||
|  | 				); | ||||||
|  |  | ||||||
|  | 				// Add uid to the upload's hash object | ||||||
|  | 				uploads = await db.getSortedSetMembers(key); | ||||||
|  | 				await db.setObjectBulk(uploads.map(relativePath => [`upload:${md5(relativePath)}`, { uid: uids[idx] }])); | ||||||
|  |  | ||||||
|  | 				progress.incr(); | ||||||
|  | 			})); | ||||||
|  | 		}, { | ||||||
|  | 			batch: 100, | ||||||
|  | 			progress: progress, | ||||||
|  | 		}); | ||||||
|  | 	}, | ||||||
|  | }; | ||||||
| @@ -3,11 +3,13 @@ | |||||||
| const path = require('path'); | const path = require('path'); | ||||||
| const nconf = require('nconf'); | const nconf = require('nconf'); | ||||||
| const winston = require('winston'); | const winston = require('winston'); | ||||||
|  | const crypto = require('crypto'); | ||||||
|  |  | ||||||
| const db = require('../database'); | const db = require('../database'); | ||||||
| const file = require('../file'); | const file = require('../file'); | ||||||
| const batch = require('../batch'); | const batch = require('../batch'); | ||||||
|  |  | ||||||
|  | const md5 = filename => crypto.createHash('md5').update(filename).digest('hex'); | ||||||
| const _getFullPath = relativePath => path.resolve(nconf.get('upload_path'), relativePath); | const _getFullPath = relativePath => path.resolve(nconf.get('upload_path'), relativePath); | ||||||
| const _validatePath = async (relativePath) => { | const _validatePath = async (relativePath) => { | ||||||
| 	const fullPath = _getFullPath(relativePath); | 	const fullPath = _getFullPath(relativePath); | ||||||
| @@ -21,7 +23,10 @@ const _validatePath = async (relativePath) => { | |||||||
| module.exports = function (User) { | module.exports = function (User) { | ||||||
| 	User.associateUpload = async (uid, relativePath) => { | 	User.associateUpload = async (uid, relativePath) => { | ||||||
| 		await _validatePath(relativePath); | 		await _validatePath(relativePath); | ||||||
| 		await db.sortedSetAdd(`uid:${uid}:uploads`, Date.now(), relativePath); | 		await Promise.all([ | ||||||
|  | 			db.sortedSetAdd(`uid:${uid}:uploads`, Date.now(), relativePath), | ||||||
|  | 			db.setObjectField(`upload:${md5(relativePath)}:uid`, uid), | ||||||
|  | 		]); | ||||||
| 	}; | 	}; | ||||||
|  |  | ||||||
| 	User.deleteUpload = async function (callerUid, uid, uploadName) { | 	User.deleteUpload = async function (callerUid, uid, uploadName) { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user