diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -2508,6 +2508,7 @@ 'PhragmentController' => 'applications/phragment/controller/PhragmentController.php', 'PhragmentCreateController' => 'applications/phragment/controller/PhragmentCreateController.php', 'PhragmentDAO' => 'applications/phragment/storage/PhragmentDAO.php', + 'PhragmentFileController' => 'applications/phragment/controller/PhragmentFileController.php', 'PhragmentFragment' => 'applications/phragment/storage/PhragmentFragment.php', 'PhragmentFragmentPHIDType' => 'applications/phragment/phid/PhragmentFragmentPHIDType.php', 'PhragmentFragmentQuery' => 'applications/phragment/query/PhragmentFragmentQuery.php', @@ -5420,6 +5421,7 @@ 'PhragmentController' => 'PhabricatorController', 'PhragmentCreateController' => 'PhragmentController', 'PhragmentDAO' => 'PhabricatorLiskDAO', + 'PhragmentFileController' => 'PhragmentController', 'PhragmentFragment' => array( 'PhragmentDAO', 'PhabricatorPolicyInterface', diff --git a/src/applications/phragment/application/PhabricatorPhragmentApplication.php b/src/applications/phragment/application/PhabricatorPhragmentApplication.php --- a/src/applications/phragment/application/PhabricatorPhragmentApplication.php +++ b/src/applications/phragment/application/PhabricatorPhragmentApplication.php @@ -45,6 +45,8 @@ 'history/(?P.+)' => 'PhragmentHistoryController', 'zip/(?P.+)' => 'PhragmentZIPController', 'zip@(?P[^/]+)/(?P.+)' => 'PhragmentZIPController', + 'file/(?P.+)' => 'PhragmentFileController', + 'file@(?P[^/]+)/(?P.+)' => 'PhragmentFileController', 'version/(?P[0-9]*)/' => 'PhragmentVersionController', 'patch/(?P[0-9x]*)/(?P[0-9]*)/' => 'PhragmentPatchController', 'revert/(?P[0-9]*)/(?P.*)' => 'PhragmentRevertController', diff --git a/src/applications/phragment/controller/PhragmentController.php b/src/applications/phragment/controller/PhragmentController.php --- a/src/applications/phragment/controller/PhragmentController.php +++ b/src/applications/phragment/controller/PhragmentController.php @@ -72,18 +72,6 @@ $this->loadHandles($phids); - $file = null; - $file_uri = null; - if (!$fragment->isDirectory()) { - $file = id(new PhabricatorFileQuery()) - ->setViewer($viewer) - ->withPHIDs(array($fragment->getLatestVersion()->getFilePHID())) - ->executeOne(); - if ($file !== null) { - $file_uri = $file->getDownloadURI(); - } - } - $header = id(new PHUIHeaderView()) ->setHeader($fragment->getName()) ->setPolicyObject($fragment) @@ -94,6 +82,10 @@ $fragment, PhabricatorPolicyCapability::CAN_EDIT); + $file_uri = null; + if (!$fragment->isDirectory()) { + $file_uri = $this->getApplicationURI('file/'.$fragment->getPath()); + } $zip_uri = $this->getApplicationURI('zip/'.$fragment->getPath()); $actions = id(new PhabricatorActionListView()) @@ -104,7 +96,8 @@ id(new PhabricatorActionView()) ->setName(pht('Download Fragment')) ->setHref($this->isCorrectlyConfigured() ? $file_uri : null) - ->setDisabled($file === null || !$this->isCorrectlyConfigured()) + ->setDisabled( + $fragment->isDirectory() || !$this->isCorrectlyConfigured()) ->setIcon('fa-download')); $actions->addAction( id(new PhabricatorActionView()) diff --git a/src/applications/phragment/controller/PhragmentFileController.php b/src/applications/phragment/controller/PhragmentFileController.php new file mode 100644 --- /dev/null +++ b/src/applications/phragment/controller/PhragmentFileController.php @@ -0,0 +1,70 @@ +dblob = idx($data, 'dblob', ''); + $this->snapshot = idx($data, 'snapshot', null); + } + + public function processRequest() { + $request = $this->getRequest(); + $viewer = $request->getUser(); + + $parents = $this->loadParentFragments($this->dblob); + if ($parents === null) { + return new Aphront404Response(); + } + $fragment = idx($parents, count($parents) - 1, null); + + if ($this->snapshot !== null) { + $snapshot = id(new PhragmentSnapshotQuery()) + ->setViewer($viewer) + ->withPrimaryFragmentPHIDs(array($fragment->getPHID())) + ->withNames(array($this->snapshot)) + ->executeOne(); + if ($snapshot === null) { + return new Aphront404Response(); + } + + $cache = id(new PhragmentSnapshotChildQuery()) + ->setViewer($viewer) + ->needFragmentVersions(true) + ->withSnapshotPHIDs(array($snapshot->getPHID())) + ->execute(); + $this->snapshotCache = mpull( + $cache, + 'getFragmentVersion', + 'getFragmentPHID'); + } + + $version = null; + if ($this->snapshot === null) { + $version = $fragment->getLatestVersion(); + } else { + $version = idx($this->snapshotCache, $fragment->getPHID(), null); + } + + $return = $fragment->getURI(); + if ($request->getExists('return')) { + $return = $request->getStr('return'); + }; + + $file = id(new PhabricatorFileQuery()) + ->setViewer($viewer) + ->withPHIDs(array($version->getFilePHID())) + ->executeOne(); + return id(new AphrontRedirectResponse()) + ->setURI($file->getDownloadURI($return)); + } + +} diff --git a/src/applications/phragment/controller/PhragmentSnapshotViewController.php b/src/applications/phragment/controller/PhragmentSnapshotViewController.php --- a/src/applications/phragment/controller/PhragmentSnapshotViewController.php +++ b/src/applications/phragment/controller/PhragmentSnapshotViewController.php @@ -99,6 +99,13 @@ ->setPolicyObject($snapshot) ->setUser($viewer); + $file_uri = null; + if (!$snapshot->getPrimaryFragment()->isDirectory()) { + $file_uri = $this->getApplicationURI( + 'file@'.$snapshot->getName(). + '/'.$snapshot->getPrimaryFragment()->getPath()); + } + $zip_uri = $this->getApplicationURI( 'zip@'.$snapshot->getName(). '/'.$snapshot->getPrimaryFragment()->getPath()); @@ -114,6 +121,14 @@ ->setObjectURI($snapshot->getURI()); $actions->addAction( id(new PhabricatorActionView()) + ->setName(pht('Download Snapshot')) + ->setHref($this->isCorrectlyConfigured() ? $file_uri : null) + ->setDisabled( + $snapshot->getPrimaryFragment()->isDirectory() || + !$this->isCorrectlyConfigured()) + ->setIcon('download')); + $actions->addAction( + id(new PhabricatorActionView()) ->setName(pht('Download Snapshot as ZIP')) ->setHref($this->isCorrectlyConfigured() ? $zip_uri : null) ->setDisabled(!$this->isCorrectlyConfigured())