mirror of
https://github.com/Chocobozzz/PeerTube.git
synced 2025-10-06 03:50:26 +02:00
Implement user import/export in server
This commit is contained in:
parent
4d63e6f577
commit
8573e5a80a
196 changed files with 5661 additions and 722 deletions
55
server/core/helpers/unzip.ts
Normal file
55
server/core/helpers/unzip.ts
Normal file
|
@ -0,0 +1,55 @@
|
|||
import { createWriteStream } from 'fs'
|
||||
import { ensureDir } from 'fs-extra/esm'
|
||||
import { dirname, join } from 'path'
|
||||
import { pipeline } from 'stream'
|
||||
import * as yauzl from 'yauzl'
|
||||
import { logger, loggerTagsFactory } from './logger.js'
|
||||
|
||||
const lTags = loggerTagsFactory('unzip')
|
||||
|
||||
export async function unzip (source: string, destination: string) {
|
||||
await ensureDir(destination)
|
||||
|
||||
logger.info(`Unzip ${source} to ${destination}`, lTags())
|
||||
|
||||
return new Promise<void>((res, rej) => {
|
||||
yauzl.open(source, { lazyEntries: true }, (err, zipFile) => {
|
||||
if (err) return rej(err)
|
||||
|
||||
zipFile.readEntry()
|
||||
|
||||
zipFile.on('entry', async entry => {
|
||||
const entryPath = join(destination, entry.fileName)
|
||||
|
||||
try {
|
||||
if (/\/$/.test(entry.fileName)) {
|
||||
await ensureDir(entryPath)
|
||||
logger.debug(`Creating directory from zip ${entryPath}`, lTags())
|
||||
|
||||
zipFile.readEntry()
|
||||
return
|
||||
}
|
||||
|
||||
await ensureDir(dirname(entryPath))
|
||||
} catch (err) {
|
||||
return rej(err)
|
||||
}
|
||||
|
||||
zipFile.openReadStream(entry, (readErr, readStream) => {
|
||||
if (readErr) return rej(readErr)
|
||||
|
||||
logger.debug(`Creating file from zip ${entryPath}`, lTags())
|
||||
|
||||
const writeStream = createWriteStream(entryPath)
|
||||
writeStream.on('close', () => zipFile.readEntry())
|
||||
|
||||
pipeline(readStream, writeStream, pipelineErr => {
|
||||
if (pipelineErr) return rej(pipelineErr)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
zipFile.on('end', () => res())
|
||||
})
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue