-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* feat: move all user profile pics to folder get rid of glob delete and just delete the uid-{uid} folder when deleting user images * when exporting user uploads add all profile uploads * uid check
- Loading branch information
1 parent
aef3ea1
commit 8f9ac5c
Showing
5 changed files
with
99 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
'use strict'; | ||
|
||
|
||
const fs = require('fs'); | ||
const nconf = require('nconf'); | ||
const path = require('path'); | ||
const { mkdirp } = require('mkdirp'); | ||
|
||
const db = require('../../database'); | ||
const batch = require('../../batch'); | ||
|
||
|
||
module.exports = { | ||
name: 'Create user upload folders', | ||
timestamp: Date.UTC(2024, 4, 28), | ||
method: async function () { | ||
const { progress } = this; | ||
|
||
const folder = path.join(nconf.get('upload_path'), 'profile'); | ||
|
||
const userPicRegex = /^\d+-profile/; | ||
const files = (await fs.promises.readdir(folder, { withFileTypes: true })) | ||
.filter(item => !item.isDirectory() && String(item.name).match(userPicRegex)) | ||
.map(item => item.name); | ||
|
||
progress.total = files.length; | ||
await batch.processArray(files, async (files) => { | ||
progress.incr(files.length); | ||
await Promise.all(files.map(async (file) => { | ||
const uid = file.split('-')[0]; | ||
if (parseInt(uid, 10) > 0) { | ||
await mkdirp(path.join(folder, `uid-${uid}`)); | ||
await fs.promises.rename( | ||
path.join(folder, file), | ||
path.join(folder, `uid-${uid}`, file), | ||
); | ||
} | ||
})); | ||
}, { | ||
batch: 500, | ||
}); | ||
|
||
await batch.processSortedSet('users:joindate', async (uids) => { | ||
progress.incr(uids.length); | ||
const usersData = await db.getObjects(uids.map(uid => `user:${uid}`)); | ||
const bulkSet = []; | ||
usersData.forEach((userData) => { | ||
const setObj = {}; | ||
if (userData && userData.picture && | ||
userData.picture.includes(`/uploads/profile/${userData.uid}-`) && | ||
!userData.picture.includes(`/uploads/profile/uid-${userData.uid}/${userData.uid}-`)) { | ||
setObj.picture = userData.picture.replace( | ||
`/uploads/profile/${userData.uid}-`, | ||
`/uploads/profile/uid-${userData.uid}/${userData.uid}-` | ||
); | ||
} | ||
|
||
if (userData && userData.uploadedpicture && | ||
userData.uploadedpicture.includes(`/uploads/profile/${userData.uid}-`) && | ||
!userData.uploadedpicture.includes(`/uploads/profile/uid-${userData.uid}/${userData.uid}-`)) { | ||
setObj.uploadedpicture = userData.uploadedpicture.replace( | ||
`/uploads/profile/${userData.uid}-`, | ||
`/uploads/profile/uid-${userData.uid}/${userData.uid}-` | ||
); | ||
} | ||
|
||
if (userData && userData['cover:url'] && | ||
userData['cover:url'].includes(`/uploads/profile/${userData.uid}-`) && | ||
!userData['cover:url'].includes(`/uploads/profile/uid-${userData.uid}/${userData.uid}-`)) { | ||
setObj['cover:url'] = userData['cover:url'].replace( | ||
`/uploads/profile/${userData.uid}-`, | ||
`/uploads/profile/uid-${userData.uid}/${userData.uid}-` | ||
); | ||
} | ||
|
||
if (Object.keys(setObj).length) { | ||
bulkSet.push([`user:${userData.uid}`, setObj]); | ||
} | ||
}); | ||
await db.setObjectBulk(bulkSet); | ||
}, { | ||
batch: 500, | ||
progress: progress, | ||
}); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters