public function testWriteUniqueFile()
 {
     $tmp = new TempFile();
     $dir = dirname($tmp);
     // Writing an empty file should work.
     $f = Filesystem::writeUniqueFile($dir, '');
     $this->assertEqual('', Filesystem::readFile($f));
     // File name should be unique.
     $g = Filesystem::writeUniqueFile($dir, 'quack');
     $this->assertTrue($f != $g);
 }
 public function run()
 {
     $conduit = $this->getConduit();
     $this->writeStatusMessage(pht('Getting file information...') . "\n");
     $info = $conduit->callMethodSynchronous('file.info', array('id' => $this->id));
     $desc = pht('(%s bytes)', new PhutilNumber($info['byteSize']));
     if ($info['name']) {
         $desc = "'" . $info['name'] . "' " . $desc;
     }
     $this->writeStatusMessage(pht('Downloading file %s...', $desc) . "\n");
     $data = $conduit->callMethodSynchronous('file.download', array('phid' => $info['phid']));
     $data = base64_decode($data);
     if ($this->show) {
         echo $data;
     } else {
         $path = Filesystem::writeUniqueFile(nonempty($this->saveAs, $info['name'], 'file'), $data);
         $this->writeStatusMessage(pht("Saved file as '%s'.", $path) . "\n");
     }
     return 0;
 }
 public function run()
 {
     $conduit = $this->getConduit();
     $this->writeStatusMessage("Getting file information...\n");
     $info = $conduit->callMethodSynchronous('file.info', array('id' => $this->id));
     $bytes = number_format($info['byteSize']);
     $desc = '(' . $bytes . ' bytes)';
     if ($info['name']) {
         $desc = "'" . $info['name'] . "' " . $desc;
     }
     $this->writeStatusMessage("Downloading file {$desc}...\n");
     $data = $conduit->callMethodSynchronous('file.download', array('phid' => $info['phid']));
     $data = base64_decode($data);
     if ($this->show) {
         echo $data;
     } else {
         $path = Filesystem::writeUniqueFile(nonempty($this->saveAs, $info['name'], 'file'), $data);
         $this->writeStatusMessage("Saved file as '{$path}'.\n");
     }
     return 0;
 }