Path: blob/master/src/applications/drydock/interface/filesystem/DrydockSFTPFilesystemInterface.php
12262 views
<?php12final class DrydockSFTPFilesystemInterface extends DrydockFilesystemInterface {34private $passphraseSSHKey;56private function openCredentialsIfNotOpen() {7if ($this->passphraseSSHKey !== null) {8return;9}1011$credential = id(new PassphraseCredentialQuery())12->setViewer(PhabricatorUser::getOmnipotentUser())13->withIDs(array($this->getConfig('credential')))14->needSecrets(true)15->executeOne();1617if ($credential->getProvidesType() !==18PassphraseSSHPrivateKeyCredentialType::PROVIDES_TYPE) {19throw new Exception(pht('Only private key credentials are supported.'));20}2122$this->passphraseSSHKey = PassphraseSSHKey::loadFromPHID(23$credential->getPHID(),24PhabricatorUser::getOmnipotentUser());25}2627private function getExecFuture($path) {28$this->openCredentialsIfNotOpen();2930return new ExecFuture(31'sftp -o "StrictHostKeyChecking no" -P %s -i %P %P@%s',32$this->getConfig('port'),33$this->passphraseSSHKey->getKeyfileEnvelope(),34$this->passphraseSSHKey->getUsernameEnvelope(),35$this->getConfig('host'));36}3738public function readFile($path) {39$target = new TempFile();40$future = $this->getExecFuture($path);41$future->write(csprintf('get %s %s', $path, $target));42$future->resolvex();43return Filesystem::readFile($target);44}4546public function saveFile($path, $name) {47$data = $this->readFile($path);48$file = PhabricatorFile::newFromFileData(49$data,50array('name' => $name));51$file->setName($name);52$file->save();53return $file;54}5556public function writeFile($path, $data) {57$source = new TempFile();58Filesystem::writeFile($source, $data);59$future = $this->getExecFuture($path);60$future->write(csprintf('put %s %s', $source, $path));61$future->resolvex();62}6364}656667