diff --git a/src/applications/auth/controller/PhabricatorAuthSSHKeyGenerateController.php b/src/applications/auth/controller/PhabricatorAuthSSHKeyGenerateController.php index 69d93548e5..070131b443 100644 --- a/src/applications/auth/controller/PhabricatorAuthSSHKeyGenerateController.php +++ b/src/applications/auth/controller/PhabricatorAuthSSHKeyGenerateController.php @@ -28,7 +28,7 @@ final class PhabricatorAuthSSHKeyGenerateController $private_key, array( 'name' => $default_name.'.key', - 'ttl' => time() + (60 * 10), + 'ttl.relative' => phutil_units('10 minutes in seconds'), 'viewPolicy' => $viewer->getPHID(), )); diff --git a/src/applications/differential/controller/DifferentialChangesetViewController.php b/src/applications/differential/controller/DifferentialChangesetViewController.php index b5bfe7464d..c2bd4bf8de 100644 --- a/src/applications/differential/controller/DifferentialChangesetViewController.php +++ b/src/applications/differential/controller/DifferentialChangesetViewController.php @@ -357,7 +357,7 @@ final class DifferentialChangesetViewController extends DifferentialController { array( 'name' => $changeset->getFilename(), 'mime-type' => 'text/plain', - 'ttl' => phutil_units('24 hours in seconds'), + 'ttl.relative' => phutil_units('24 hours in seconds'), 'viewPolicy' => PhabricatorPolicies::POLICY_NOONE, )); diff --git a/src/applications/differential/controller/DifferentialRevisionViewController.php b/src/applications/differential/controller/DifferentialRevisionViewController.php index e7d0a1cc87..26ff22838f 100644 --- a/src/applications/differential/controller/DifferentialRevisionViewController.php +++ b/src/applications/differential/controller/DifferentialRevisionViewController.php @@ -893,7 +893,7 @@ final class DifferentialRevisionViewController extends DifferentialController { $raw_diff, array( 'name' => $file_name, - 'ttl' => (60 * 60 * 24), + 'ttl.relative' => phutil_units('24 hours in seconds'), 'viewPolicy' => PhabricatorPolicies::POLICY_NOONE, )); diff --git a/src/applications/diffusion/query/DiffusionFileFutureQuery.php b/src/applications/diffusion/query/DiffusionFileFutureQuery.php index 8ce59028e2..250e4962da 100644 --- a/src/applications/diffusion/query/DiffusionFileFutureQuery.php +++ b/src/applications/diffusion/query/DiffusionFileFutureQuery.php @@ -93,7 +93,7 @@ abstract class DiffusionFileFutureQuery $drequest = $this->getRequest(); $name = basename($drequest->getPath()); - $ttl = PhabricatorTime::getNow() + phutil_units('48 hours in seconds'); + $relative_ttl = phutil_units('48 hours in seconds'); try { $threshold = PhabricatorFileStorageEngine::getChunkThreshold(); @@ -101,7 +101,7 @@ abstract class DiffusionFileFutureQuery $source = id(new PhabricatorExecFutureFileUploadSource()) ->setName($name) - ->setTTL($ttl) + ->setRelativeTTL($relative_ttl) ->setViewPolicy(PhabricatorPolicies::POLICY_NOONE) ->setExecFuture($future); diff --git a/src/applications/files/PhabricatorImageTransformer.php b/src/applications/files/PhabricatorImageTransformer.php index 75e83c223d..df4243cfdd 100644 --- a/src/applications/files/PhabricatorImageTransformer.php +++ b/src/applications/files/PhabricatorImageTransformer.php @@ -15,7 +15,7 @@ final class PhabricatorImageTransformer extends Phobject { $image, array( 'name' => 'meme-'.$file->getName(), - 'ttl' => time() + 60 * 60 * 24, + 'ttl.relative' => phutil_units('24 hours in seconds'), 'canCDN' => true, )); } diff --git a/src/applications/files/conduit/FileAllocateConduitAPIMethod.php b/src/applications/files/conduit/FileAllocateConduitAPIMethod.php index ff3a0c80da..ddc5fa14fe 100644 --- a/src/applications/files/conduit/FileAllocateConduitAPIMethod.php +++ b/src/applications/files/conduit/FileAllocateConduitAPIMethod.php @@ -42,7 +42,7 @@ final class FileAllocateConduitAPIMethod $ttl = $request->getValue('deleteAfterEpoch'); if ($ttl) { - $properties['ttl'] = $ttl; + $properties['ttl.absolute'] = $ttl; } $file = null; diff --git a/src/applications/files/controller/PhabricatorFileInfoController.php b/src/applications/files/controller/PhabricatorFileInfoController.php index 8e92423fbc..7217f284b4 100644 --- a/src/applications/files/controller/PhabricatorFileInfoController.php +++ b/src/applications/files/controller/PhabricatorFileInfoController.php @@ -212,6 +212,18 @@ final class PhabricatorFileInfoController extends PhabricatorFileController { pht('Mime Type'), $file->getMimeType()); + $ttl = $file->getTtl(); + if ($ttl) { + $delta = $ttl - PhabricatorTime::getNow(); + + $finfo->addProperty( + pht('Expires'), + pht( + '%s (%s)', + phabricator_datetime($ttl, $viewer), + phutil_format_relative_time_detailed($delta))); + } + $width = $file->getImageWidth(); if ($width) { $finfo->addProperty( diff --git a/src/applications/files/storage/PhabricatorFile.php b/src/applications/files/storage/PhabricatorFile.php index 3cd734ba5e..4136f1fdf8 100644 --- a/src/applications/files/storage/PhabricatorFile.php +++ b/src/applications/files/storage/PhabricatorFile.php @@ -9,10 +9,13 @@ * * | name | Human readable filename. * | authorPHID | User PHID of uploader. - * | ttl | Temporary file lifetime, in seconds. + * | ttl.absolute | Temporary file lifetime as an epoch timestamp. + * | ttl.relative | Temporary file lifetime, relative to now, in seconds. * | viewPolicy | File visibility policy. * | isExplicitUpload | Used to show users files they explicitly uploaded. * | canCDN | Allows the file to be cached and delivered over a CDN. + * | profile | Marks the file as a profile image. + * | format | Internal encoding format. * | mime-type | Optional, explicit file MIME type. * | builtin | Optional filename, identifies this as a builtin. * @@ -1110,7 +1113,7 @@ final class PhabricatorFile extends PhabricatorFileDAO $params = array( 'name' => $builtin->getBuiltinDisplayName(), - 'ttl' => time() + (60 * 60 * 24 * 7), + 'ttl.relative' => phutil_units('7 days in seconds'), 'canCDN' => true, 'builtin' => $key, ); @@ -1280,14 +1283,68 @@ final class PhabricatorFile extends PhabricatorFileDAO * @return this */ private function readPropertiesFromParameters(array $params) { + PhutilTypeSpec::checkMap( + $params, + array( + 'name' => 'optional string', + 'authorPHID' => 'optional string', + 'ttl.relative' => 'optional int', + 'ttl.absolute' => 'optional int', + 'viewPolicy' => 'optional string', + 'isExplicitUpload' => 'optional bool', + 'canCDN' => 'optional bool', + 'profile' => 'optional bool', + 'format' => 'optional string|PhabricatorFileStorageFormat', + 'mime-type' => 'optional string', + 'builtin' => 'optional string', + 'storageEngines' => 'optional list', + )); + $file_name = idx($params, 'name'); $this->setName($file_name); $author_phid = idx($params, 'authorPHID'); $this->setAuthorPHID($author_phid); - $file_ttl = idx($params, 'ttl'); - $this->setTtl($file_ttl); + $absolute_ttl = idx($params, 'ttl.absolute'); + $relative_ttl = idx($params, 'ttl.relative'); + if ($absolute_ttl !== null && $relative_ttl !== null) { + throw new Exception( + pht( + 'Specify an absolute TTL or a relative TTL, but not both.')); + } else if ($absolute_ttl !== null) { + if ($absolute_ttl < PhabricatorTime::getNow()) { + throw new Exception( + pht( + 'Absolute TTL must be in the present or future, but TTL "%s" '. + 'is in the past.', + $absolute_ttl)); + } + + $this->setTtl($absolute_ttl); + } else if ($relative_ttl !== null) { + if ($relative_ttl < 0) { + throw new Exception( + pht( + 'Relative TTL must be zero or more seconds, but "%s" is '. + 'negative.', + $relative_ttl)); + } + + $max_relative = phutil_units('365 days in seconds'); + if ($relative_ttl > $max_relative) { + throw new Exception( + pht( + 'Relative TTL must not be more than "%s" seconds, but TTL '. + '"%s" was specified.', + $max_relative, + $relative_ttl)); + } + + $absolute_ttl = PhabricatorTime::getNow() + $relative_ttl; + + $this->setTtl($absolute_ttl); + } $view_policy = idx($params, 'viewPolicy'); if ($view_policy) { diff --git a/src/applications/files/storage/__tests__/PhabricatorFileTestCase.php b/src/applications/files/storage/__tests__/PhabricatorFileTestCase.php index f9090c9c83..5b64c9b1ac 100644 --- a/src/applications/files/storage/__tests__/PhabricatorFileTestCase.php +++ b/src/applications/files/storage/__tests__/PhabricatorFileTestCase.php @@ -370,11 +370,11 @@ final class PhabricatorFileTestCase extends PhabricatorTestCase { $data = Filesystem::readRandomCharacters(64); - $ttl = (time() + 60 * 60 * 24); + $ttl = (PhabricatorTime::getNow() + phutil_units('24 hours in seconds')); $params = array( 'name' => 'test.dat', - 'ttl' => ($ttl), + 'ttl.absolute' => $ttl, 'storageEngines' => array( $engine, ), diff --git a/src/applications/files/uploadsource/PhabricatorFileUploadSource.php b/src/applications/files/uploadsource/PhabricatorFileUploadSource.php index 1d38402066..b4578e9e56 100644 --- a/src/applications/files/uploadsource/PhabricatorFileUploadSource.php +++ b/src/applications/files/uploadsource/PhabricatorFileUploadSource.php @@ -4,7 +4,7 @@ abstract class PhabricatorFileUploadSource extends Phobject { private $name; - private $ttl; + private $relativeTTL; private $viewPolicy; private $rope; @@ -22,13 +22,13 @@ abstract class PhabricatorFileUploadSource return $this->name; } - public function setTTL($ttl) { - $this->ttl = $ttl; + public function setRelativeTTL($relative_ttl) { + $this->relativeTTL = $relative_ttl; return $this; } - public function getTTL() { - return $this->ttl; + public function getRelativeTTL() { + return $this->relativeTTL; } public function setViewPolicy($view_policy) { @@ -214,7 +214,7 @@ abstract class PhabricatorFileUploadSource private function getNewFileParameters() { return array( 'name' => $this->getName(), - 'ttl' => $this->getTTL(), + 'ttl.relative' => $this->getRelativeTTL(), 'viewPolicy' => $this->getViewPolicy(), ); } diff --git a/src/applications/phragment/conduit/PhragmentGetPatchConduitAPIMethod.php b/src/applications/phragment/conduit/PhragmentGetPatchConduitAPIMethod.php index aa9e008007..0a3736f93a 100644 --- a/src/applications/phragment/conduit/PhragmentGetPatchConduitAPIMethod.php +++ b/src/applications/phragment/conduit/PhragmentGetPatchConduitAPIMethod.php @@ -178,7 +178,7 @@ final class PhragmentGetPatchConduitAPIMethod $data, array( 'name' => 'patch.dmp', - 'ttl' => time() + 60 * 60 * 24, + 'ttl.relative' => phutil_units('24 hours in seconds'), )); $patches[$key]['patchURI'] = $file->getDownloadURI(); } diff --git a/src/applications/phragment/controller/PhragmentPatchController.php b/src/applications/phragment/controller/PhragmentPatchController.php index 5ad4b6dfec..dade7b2f89 100644 --- a/src/applications/phragment/controller/PhragmentPatchController.php +++ b/src/applications/phragment/controller/PhragmentPatchController.php @@ -83,7 +83,7 @@ final class PhragmentPatchController extends PhragmentController { array( 'name' => $name, 'mime-type' => 'text/plain', - 'ttl' => time() + 60 * 60 * 24, + 'ttl.relative' => phutil_units('24 hours in seconds'), )); $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites(); diff --git a/src/applications/phragment/controller/PhragmentZIPController.php b/src/applications/phragment/controller/PhragmentZIPController.php index 75d464d9c8..6cb9d3ac77 100644 --- a/src/applications/phragment/controller/PhragmentZIPController.php +++ b/src/applications/phragment/controller/PhragmentZIPController.php @@ -104,7 +104,7 @@ final class PhragmentZIPController extends PhragmentController { $data, array( 'name' => $zip_name, - 'ttl' => time() + 60 * 60 * 24, + 'ttl.relative' => phutil_units('24 hours in seconds'), )); $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites();