public function execute(PhutilArgumentParser $args)
 {
     $iterator = $this->buildIterator($args);
     if (!$iterator) {
         throw new PhutilArgumentUsageException(pht('Either specify a list of files to encode, or use --all to ' . 'encode all files.'));
     }
     $force = (bool) $args->getArg('force');
     $format_list = PhabricatorFileStorageFormat::getAllFormats();
     $format_list = array_keys($format_list);
     $format_list = implode(', ', $format_list);
     $format_key = $args->getArg('as');
     if (!strlen($format_key)) {
         throw new PhutilArgumentUsageException(pht('Use --as <format> to select a target encoding format. Available ' . 'formats are: %s.', $format_list));
     }
     $format = PhabricatorFileStorageFormat::getFormat($format_key);
     if (!$format) {
         throw new PhutilArgumentUsageException(pht('Storage format "%s" is not valid. Available formats are: %s.', $format_key, $format_list));
     }
     $key_name = $args->getArg('key');
     if (strlen($key_name)) {
         $format->selectMasterKey($key_name);
     }
     $engines = PhabricatorFileStorageEngine::loadAllEngines();
     $failed = array();
     foreach ($iterator as $file) {
         $monogram = $file->getMonogram();
         $engine_key = $file->getStorageEngine();
         $engine = idx($engines, $engine_key);
         if (!$engine) {
             echo tsprintf("%s\n", pht('%s: Uses unknown storage engine "%s".', $monogram, $engine_key));
             $failed[] = $file;
             continue;
         }
         if ($engine->isChunkEngine()) {
             echo tsprintf("%s\n", pht('%s: Stored as chunks, no data to encode directly.', $monogram));
             continue;
         }
         if ($file->getStorageFormat() == $format_key && !$force) {
             echo tsprintf("%s\n", pht('%s: Already encoded in target format.', $monogram));
             continue;
         }
         echo tsprintf("%s\n", pht('%s: Changing encoding from "%s" to "%s".', $monogram, $file->getStorageFormat(), $format_key));
         try {
             $file->migrateToStorageFormat($format);
             echo tsprintf("%s\n", pht('Done.'));
         } catch (Exception $ex) {
             echo tsprintf("%B\n", pht('Failed! %s', (string) $ex));
             $failed[] = $file;
         }
     }
     if ($failed) {
         $monograms = mpull($failed, 'getMonogram');
         echo tsprintf("%s\n", pht('Failures: %s.', implode(', ', $monograms)));
         return 1;
     }
     return 0;
 }
 public function execute(PhutilArgumentParser $args)
 {
     $type = $args->getArg('type');
     if (!strlen($type)) {
         throw new PhutilArgumentUsageException(pht('Specify the type of key to generate with --type.'));
     }
     $format = PhabricatorFileStorageFormat::getFormat($type);
     if (!$format) {
         throw new PhutilArgumentUsageException(pht('No key type "%s" exists.', $type));
     }
     if (!$format->canGenerateNewKeyMaterial()) {
         throw new PhutilArgumentUsageException(pht('Storage format "%s" can not generate keys.', $format->getStorageFormatName()));
     }
     $material = $format->generateNewKeyMaterial();
     $structure = array('name' => 'generated-key-' . Filesystem::readRandomCharacters(12), 'type' => $type, 'material.base64' => $material);
     $json = id(new PhutilJSON())->encodeFormatted($structure);
     echo tsprintf("%s: %s\n\n%B\n", pht('Key Material'), $format->getStorageFormatName(), $json);
     return 0;
 }
 private function buildPropertyViews(PHUIObjectBoxView $box, PhabricatorFile $file)
 {
     $request = $this->getRequest();
     $viewer = $request->getUser();
     $tab_group = id(new PHUITabGroupView());
     $box->addTabGroup($tab_group);
     $properties = id(new PHUIPropertyListView());
     $tab_group->addTab(id(new PHUITabView())->setName(pht('Details'))->setKey('details')->appendChild($properties));
     if ($file->getAuthorPHID()) {
         $properties->addProperty(pht('Author'), $viewer->renderHandle($file->getAuthorPHID()));
     }
     $properties->addProperty(pht('Created'), phabricator_datetime($file->getDateCreated(), $viewer));
     $finfo = id(new PHUIPropertyListView());
     $tab_group->addTab(id(new PHUITabView())->setName(pht('File Info'))->setKey('info')->appendChild($finfo));
     $finfo->addProperty(pht('Size'), phutil_format_bytes($file->getByteSize()));
     $finfo->addProperty(pht('Mime Type'), $file->getMimeType());
     $width = $file->getImageWidth();
     if ($width) {
         $finfo->addProperty(pht('Width'), pht('%s px', new PhutilNumber($width)));
     }
     $height = $file->getImageHeight();
     if ($height) {
         $finfo->addProperty(pht('Height'), pht('%s px', new PhutilNumber($height)));
     }
     $is_image = $file->isViewableImage();
     if ($is_image) {
         $image_string = pht('Yes');
         $cache_string = $file->getCanCDN() ? pht('Yes') : pht('No');
     } else {
         $image_string = pht('No');
         $cache_string = pht('Not Applicable');
     }
     $types = array();
     if ($file->isViewableImage()) {
         $types[] = pht('Image');
     }
     if ($file->isVideo()) {
         $types[] = pht('Video');
     }
     if ($file->isAudio()) {
         $types[] = pht('Audio');
     }
     if ($file->getCanCDN()) {
         $types[] = pht('Can CDN');
     }
     $builtin = $file->getBuiltinName();
     if ($builtin !== null) {
         $types[] = pht('Builtin ("%s")', $builtin);
     }
     if ($file->getIsProfileImage()) {
         $types[] = pht('Profile');
     }
     if ($types) {
         $types = implode(', ', $types);
         $finfo->addProperty(pht('Attributes'), $types);
     }
     $storage_properties = new PHUIPropertyListView();
     $tab_group->addTab(id(new PHUITabView())->setName(pht('Storage'))->setKey('storage')->appendChild($storage_properties));
     $storage_properties->addProperty(pht('Engine'), $file->getStorageEngine());
     $engine = $this->loadStorageEngine($file);
     if ($engine && $engine->isChunkEngine()) {
         $format_name = pht('Chunks');
     } else {
         $format_key = $file->getStorageFormat();
         $format = PhabricatorFileStorageFormat::getFormat($format_key);
         if ($format) {
             $format_name = $format->getStorageFormatName();
         } else {
             $format_name = pht('Unknown ("%s")', $format_key);
         }
     }
     $storage_properties->addProperty(pht('Format'), $format_name);
     $storage_properties->addProperty(pht('Handle'), $file->getStorageHandle());
     $phids = $file->getObjectPHIDs();
     if ($phids) {
         $attached = new PHUIPropertyListView();
         $tab_group->addTab(id(new PHUITabView())->setName(pht('Attached'))->setKey('attached')->appendChild($attached));
         $attached->addProperty(pht('Attached To'), $viewer->renderHandleList($phids));
     }
     if ($file->isViewableImage()) {
         $image = phutil_tag('img', array('src' => $file->getViewURI(), 'class' => 'phui-property-list-image'));
         $linked_image = phutil_tag('a', array('href' => $file->getViewURI()), $image);
         $media = id(new PHUIPropertyListView())->addImageContent($linked_image);
         $box->addPropertyList($media);
     } else {
         if ($file->isVideo()) {
             $video = phutil_tag('video', array('controls' => 'controls', 'class' => 'phui-property-list-video'), phutil_tag('source', array('src' => $file->getViewURI(), 'type' => $file->getMimeType())));
             $media = id(new PHUIPropertyListView())->addImageContent($video);
             $box->addPropertyList($media);
         } else {
             if ($file->isAudio()) {
                 $audio = phutil_tag('audio', array('controls' => 'controls', 'class' => 'phui-property-list-audio'), phutil_tag('source', array('src' => $file->getViewURI(), 'type' => $file->getMimeType())));
                 $media = id(new PHUIPropertyListView())->addImageContent($audio);
                 $box->addPropertyList($media);
             }
         }
     }
     $engine = $this->loadStorageEngine($file);
     if ($engine) {
         if ($engine->isChunkEngine()) {
             $chunkinfo = new PHUIPropertyListView();
             $tab_group->addTab(id(new PHUITabView())->setName(pht('Chunks'))->setKey('chunks')->appendChild($chunkinfo));
             $chunks = id(new PhabricatorFileChunkQuery())->setViewer($viewer)->withChunkHandles(array($file->getStorageHandle()))->execute();
             $chunks = msort($chunks, 'getByteStart');
             $rows = array();
             $completed = array();
             foreach ($chunks as $chunk) {
                 $is_complete = $chunk->getDataFilePHID();
                 $rows[] = array($chunk->getByteStart(), $chunk->getByteEnd(), $is_complete ? pht('Yes') : pht('No'));
                 if ($is_complete) {
                     $completed[] = $chunk;
                 }
             }
             $table = id(new AphrontTableView($rows))->setHeaders(array(pht('Offset'), pht('End'), pht('Complete')))->setColumnClasses(array('', '', 'wide'));
             $chunkinfo->addProperty(pht('Total Chunks'), count($chunks));
             $chunkinfo->addProperty(pht('Completed Chunks'), count($completed));
             $chunkinfo->addRawContent($table);
         }
     }
 }