2013-12-08 02:14:34 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
final class PhragmentPatchController extends PhragmentController {
|
|
|
|
|
2013-12-13 04:42:12 +01:00
|
|
|
public function shouldAllowPublic() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-04-02 16:24:40 +02:00
|
|
|
public function handleRequest(AphrontRequest $request) {
|
|
|
|
$viewer = $request->getViewer();
|
|
|
|
$aid = $request->getURIData('aid');
|
|
|
|
$bid = $request->getURIData('bid');
|
2013-12-08 02:14:34 +01:00
|
|
|
|
|
|
|
// If "aid" is "x", then it means the user wants to generate
|
|
|
|
// a patch of an empty file to the version specified by "bid".
|
|
|
|
|
2016-04-02 16:24:40 +02:00
|
|
|
$ids = array($aid, $bid);
|
|
|
|
if ($aid === 'x') {
|
|
|
|
$ids = array($bid);
|
2013-12-08 02:14:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$versions = id(new PhragmentFragmentVersionQuery())
|
|
|
|
->setViewer($viewer)
|
|
|
|
->withIDs($ids)
|
|
|
|
->execute();
|
|
|
|
|
|
|
|
$version_a = null;
|
2016-04-02 16:24:40 +02:00
|
|
|
if ($aid !== 'x') {
|
|
|
|
$version_a = idx($versions, $aid, null);
|
2013-12-08 02:14:34 +01:00
|
|
|
if ($version_a === null) {
|
|
|
|
return new Aphront404Response();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-02 16:24:40 +02:00
|
|
|
$version_b = idx($versions, $bid, null);
|
2013-12-08 02:14:34 +01:00
|
|
|
if ($version_b === null) {
|
|
|
|
return new Aphront404Response();
|
|
|
|
}
|
|
|
|
|
|
|
|
$file_phids = array();
|
|
|
|
if ($version_a !== null) {
|
|
|
|
$file_phids[] = $version_a->getFilePHID();
|
|
|
|
}
|
|
|
|
$file_phids[] = $version_b->getFilePHID();
|
|
|
|
|
|
|
|
$files = id(new PhabricatorFileQuery())
|
|
|
|
->setViewer($viewer)
|
|
|
|
->withPHIDs($file_phids)
|
|
|
|
->execute();
|
|
|
|
$files = mpull($files, null, 'getPHID');
|
|
|
|
|
|
|
|
$file_a = null;
|
|
|
|
if ($version_a != null) {
|
|
|
|
$file_a = idx($files, $version_a->getFilePHID(), null);
|
|
|
|
}
|
|
|
|
$file_b = idx($files, $version_b->getFilePHID(), null);
|
|
|
|
|
|
|
|
$patch = PhragmentPatchUtil::calculatePatch($file_a, $file_b);
|
|
|
|
|
|
|
|
if ($patch === null) {
|
2013-12-13 04:42:12 +01:00
|
|
|
// There are no differences between the two files, so we output
|
|
|
|
// an empty patch.
|
|
|
|
$patch = '';
|
2013-12-08 02:14:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$a_sequence = 'x';
|
|
|
|
if ($version_a !== null) {
|
|
|
|
$a_sequence = $version_a->getSequence();
|
|
|
|
}
|
|
|
|
|
|
|
|
$name =
|
|
|
|
$version_b->getFragment()->getName().'.'.
|
|
|
|
$a_sequence.'.'.
|
|
|
|
$version_b->getSequence().'.patch';
|
|
|
|
|
2013-12-13 04:42:12 +01:00
|
|
|
$return = $version_b->getURI();
|
|
|
|
if ($request->getExists('return')) {
|
|
|
|
$return = $request->getStr('return');
|
|
|
|
}
|
|
|
|
|
2013-12-08 02:14:34 +01:00
|
|
|
$result = PhabricatorFile::buildFromFileDataOrHash(
|
|
|
|
$patch,
|
|
|
|
array(
|
|
|
|
'name' => $name,
|
|
|
|
'mime-type' => 'text/plain',
|
Make the Files "TTL" API more structured
Summary:
Ref T11357. When creating a file, callers can currently specify a `ttl`. However, it isn't unambiguous what you're supposed to pass, and some callers get it wrong.
For example, to mean "this file expires in 60 minutes", you might pass either of these:
- `time() + phutil_units('60 minutes in seconds')`
- `phutil_units('60 minutes in seconds')`
The former means "60 minutes from now". The latter means "1 AM, January 1, 1970". In practice, because the GC normally runs only once every four hours (at least, until recently), and all the bad TTLs are cases where files are normally accessed immediately, these 1970 TTLs didn't cause any real problems.
Split `ttl` into `ttl.relative` and `ttl.absolute`, and make sure the values are sane. Then correct all callers, and simplify out the `time()` calls where possible to make switching to `PhabricatorTime` easier.
Test Plan:
- Generated an SSH keypair.
- Viewed a changeset.
- Viewed a raw diff.
- Viewed a commit's file data.
- Viewed a temporary file's details, saw expiration date and relative time.
- Ran unit tests.
- (Didn't really test Phragment.)
Reviewers: chad
Reviewed By: chad
Subscribers: hach-que
Maniphest Tasks: T11357
Differential Revision: https://secure.phabricator.com/D17616
2017-04-04 20:01:43 +02:00
|
|
|
'ttl.relative' => phutil_units('24 hours in seconds'),
|
2013-12-08 02:14:34 +01:00
|
|
|
));
|
2013-12-13 04:42:12 +01:00
|
|
|
|
|
|
|
$unguarded = AphrontWriteGuard::beginScopedUnguardedWrites();
|
2014-09-04 21:51:33 +02:00
|
|
|
$result->attachToObject($version_b->getFragmentPHID());
|
2013-12-13 04:42:12 +01:00
|
|
|
unset($unguarded);
|
|
|
|
|
2013-12-08 02:14:34 +01:00
|
|
|
return id(new AphrontRedirectResponse())
|
2013-12-13 04:42:12 +01:00
|
|
|
->setURI($result->getDownloadURI($return));
|
2013-12-08 02:14:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|