Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ftp download corrections #1614

Merged
merged 8 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading