Skip to content

Commit

Permalink
Merge pull request #1614 from oliviermgx/silex-ftpConnector
Browse files Browse the repository at this point in the history
ftp download corrections
  • Loading branch information
lexoyo authored Sep 25, 2024
2 parents 2700b92 + 406be6d commit 3526840
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
22 changes: 11 additions & 11 deletions src/ts/plugins/server/FtpConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/

import { Client } from 'basic-ftp'
import { PassThrough, Readable, Writable } from 'stream'
import { Readable } from 'stream'
import { ConnectorFile, ConnectorFileContent, HostingConnector, StatusCallback, StorageConnector, contentToReadable, contentToString, toConnectorData, toConnectorEnum} from '../../server/connectors/connectors'
import { requiredParam } from '../../server/utils/validation'
import { WEBSITE_DATA_FILE, WEBSITE_META_DATA_FILE } from '../../constants'
Expand All @@ -26,7 +26,8 @@ import { join } from 'path'
import { tmpdir } from 'os'
import { v4 as uuid } from 'uuid'
import { JobManager } from '../../server/jobs'
import { mkdtemp } from 'fs/promises'
import { mkdtemp, rm, rmdir } from 'fs/promises'
import { createReadStream } from 'fs'

/**
* @fileoverview FTP connector for Silex
Expand Down Expand Up @@ -273,9 +274,13 @@ export default class FtpConnector implements StorageConnector<FtpSession> {
}

private async read(ftp: Client, path: string): Promise<Readable> {
const stream = new PassThrough()
await ftp.downloadTo(stream, path)
return stream
const tempDir = await mkdtemp(join(tmpdir(), 'silex-tmp'))
const tempPath = join(tempDir, 'silex-ftp.tmp')
await ftp.downloadTo(tempPath, path)
const rStream = createReadStream(tempPath)
await rm(tempPath)
await rmdir(tempDir)
return rStream
}

private async readdir(ftp: Client, path: string): Promise<FileMeta[]> {
Expand Down Expand Up @@ -404,18 +409,13 @@ export default class FtpConnector implements StorageConnector<FtpSession> {
const ftp = await this.getClient(this.sessionData(session))
// Get stats for the website folder
const folder = join(this.rootPath(session), websiteId)
const lastMod = await ftp.lastMod(folder)
// Read the meta data file to get the meta data set by the user
const path = join(this.rootPath(session), websiteId, WEBSITE_META_DATA_FILE)
const readable = await this.read(ftp, path)
const meta = JSON.parse(await contentToString(readable)) as WebsiteMetaFileContent
this.closeClient(ftp)
// Return all meta
return {
websiteId,
//url: await this.getFileUrl(session, websiteId, WEBSITE_DATA_FILE_NAME),
createdAt: lastMod,
updatedAt: lastMod,
...meta,
}
} catch(err) {
Expand Down Expand Up @@ -570,7 +570,7 @@ export default class FtpConnector implements StorageConnector<FtpSession> {
const storageRootPath = this.rootPath(session)
const ftp = await this.getClient(this.sessionData(session))
const dirPath = join(this.options.path, storageRootPath, id, this.options.assetsFolder, path)
const asset = this.read(ftp, dirPath)
const asset = await this.read(ftp, dirPath)
this.closeClient(ftp)
return asset
}
Expand Down
4 changes: 2 additions & 2 deletions src/ts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,8 @@ export interface WebsiteMetaFileContent {
*/
export interface WebsiteMeta extends WebsiteMetaFileContent {
websiteId: WebsiteId
createdAt: Date
updatedAt: Date
createdAt?: Date
updatedAt?: Date
}

/**
Expand Down

0 comments on commit 3526840

Please sign in to comment.