| 39 | } |
| 40 | |
| 41 | private async edit (item: SFTPFile, sftp: SFTPSession) { |
| 42 | const tempDir = (await tmp.dir({ unsafeCleanup: true })).path |
| 43 | const tempPath = path.join(tempDir, item.name) |
| 44 | const transfer = await this.platform.startDownload(item.name, item.mode, item.size, tempPath) |
| 45 | if (!transfer) { |
| 46 | return |
| 47 | } |
| 48 | await sftp.download(item.fullPath, transfer) |
| 49 | this.platform.openPath(tempPath) |
| 50 | |
| 51 | const events = new Subject<string>() |
| 52 | fs.chmodSync(tempPath, 0o700) |
| 53 | |
| 54 | // skip the first burst of events |
| 55 | setTimeout(() => { |
| 56 | const watcher = fs.watch(tempPath, event => events.next(event)) |
| 57 | events.pipe(debounceTime(1000), debounce(async event => { |
| 58 | if (event === 'rename') { |
| 59 | watcher.close() |
| 60 | } |
| 61 | const upload = await this.platform.startUpload({ multiple: false }, [tempPath]) |
| 62 | if (!upload.length) { |
| 63 | return |
| 64 | } |
| 65 | await sftp.upload(item.fullPath, upload[0]) |
| 66 | await sftp.chmod(item.fullPath, item.mode) |
| 67 | })).subscribe() |
| 68 | watcher.on('close', () => events.complete()) |
| 69 | sftp.closed$.subscribe(() => watcher.close()) |
| 70 | }, 1000) |
| 71 | } |
| 72 | } |