2011-05-22 23:40:51 +02:00
|
|
|
<?php
|
|
|
|
|
2012-03-10 00:46:25 +01:00
|
|
|
final class PhabricatorFileTransformController
|
|
|
|
extends PhabricatorFileController {
|
2011-05-22 23:40:51 +02:00
|
|
|
|
|
|
|
private $transform;
|
|
|
|
private $phid;
|
2012-10-24 02:34:43 +02:00
|
|
|
private $key;
|
2011-05-22 23:40:51 +02:00
|
|
|
|
2013-10-05 21:55:34 +02:00
|
|
|
public function shouldRequireLogin() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-05-22 23:40:51 +02:00
|
|
|
public function willProcessRequest(array $data) {
|
|
|
|
$this->transform = $data['transform'];
|
2012-10-23 04:06:56 +02:00
|
|
|
$this->phid = $data['phid'];
|
|
|
|
$this->key = $data['key'];
|
|
|
|
}
|
|
|
|
|
2011-05-22 23:40:51 +02:00
|
|
|
public function processRequest() {
|
2013-09-30 18:38:13 +02:00
|
|
|
$viewer = $this->getRequest()->getUser();
|
2011-05-22 23:40:51 +02:00
|
|
|
|
2013-10-05 21:55:34 +02:00
|
|
|
// NOTE: This is a public/CDN endpoint, and permission to see files is
|
|
|
|
// controlled by knowing the secret key, not by authentication.
|
|
|
|
|
2013-09-30 18:38:13 +02:00
|
|
|
$file = id(new PhabricatorFileQuery())
|
2013-10-05 21:55:34 +02:00
|
|
|
->setViewer(PhabricatorUser::getOmnipotentUser())
|
2013-09-30 18:38:13 +02:00
|
|
|
->withPHIDs(array($this->phid))
|
|
|
|
->executeOne();
|
2012-10-23 04:06:56 +02:00
|
|
|
if (!$file) {
|
|
|
|
return new Aphront404Response();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$file->validateSecretKey($this->key)) {
|
|
|
|
return new Aphront403Response();
|
|
|
|
}
|
|
|
|
|
2011-05-22 23:40:51 +02:00
|
|
|
$xform = id(new PhabricatorTransformedFile())
|
|
|
|
->loadOneWhere(
|
|
|
|
'originalPHID = %s AND transform = %s',
|
|
|
|
$this->phid,
|
|
|
|
$this->transform);
|
|
|
|
|
|
|
|
if ($xform) {
|
|
|
|
return $this->buildTransformedFileResponse($xform);
|
|
|
|
}
|
|
|
|
|
Support thumbnailing non-image files and straighten out setup for 'gd'
Summary:
Make 'gd' an explicit optional dependency, test for it in setup, and make the
software behave correctly if it is not available.
When generating file thumnails, provide reasonable defaults and behavior for
non-image files.
Test Plan:
Uploaded text files, pdf files, etc., and got real thumbnails instead of a
broken image.
Simulated setup and gd failures and walked through setup process and image
fallback for thumbnails.
Reviewed By: aran
Reviewers: toulouse, jungejason, tuomaspelkonen, aran
CC: aran, epriestley
Differential Revision: 446
2011-06-13 17:43:42 +02:00
|
|
|
$type = $file->getMimeType();
|
2011-05-22 23:40:51 +02:00
|
|
|
|
Support thumbnailing non-image files and straighten out setup for 'gd'
Summary:
Make 'gd' an explicit optional dependency, test for it in setup, and make the
software behave correctly if it is not available.
When generating file thumnails, provide reasonable defaults and behavior for
non-image files.
Test Plan:
Uploaded text files, pdf files, etc., and got real thumbnails instead of a
broken image.
Simulated setup and gd failures and walked through setup process and image
fallback for thumbnails.
Reviewed By: aran
Reviewers: toulouse, jungejason, tuomaspelkonen, aran
CC: aran, epriestley
Differential Revision: 446
2011-06-13 17:43:42 +02:00
|
|
|
if (!$file->isViewableInBrowser() || !$file->isTransformableImage()) {
|
|
|
|
return $this->buildDefaultTransformation($file);
|
2011-05-22 23:40:51 +02:00
|
|
|
}
|
|
|
|
|
2011-08-17 23:39:00 +02:00
|
|
|
// We're essentially just building a cache here and don't need CSRF
|
|
|
|
// protection.
|
|
|
|
$unguarded = AphrontWriteGuard::beginScopedUnguardedWrites();
|
|
|
|
|
2011-05-22 23:40:51 +02:00
|
|
|
switch ($this->transform) {
|
2013-06-17 16:08:50 +02:00
|
|
|
case 'thumb-profile':
|
|
|
|
$xformed_file = $this->executeThumbTransform($file, 50, 50);
|
|
|
|
break;
|
2013-03-05 17:05:51 +01:00
|
|
|
case 'thumb-280x210':
|
|
|
|
$xformed_file = $this->executeThumbTransform($file, 280, 210);
|
|
|
|
break;
|
2012-10-08 22:26:10 +02:00
|
|
|
case 'thumb-220x165':
|
|
|
|
$xformed_file = $this->executeThumbTransform($file, 220, 165);
|
|
|
|
break;
|
2013-02-22 15:53:54 +01:00
|
|
|
case 'preview-140':
|
|
|
|
$xformed_file = $this->executePreviewTransform($file, 140);
|
|
|
|
break;
|
2012-10-08 22:26:10 +02:00
|
|
|
case 'preview-220':
|
|
|
|
$xformed_file = $this->executePreviewTransform($file, 220);
|
|
|
|
break;
|
2011-05-22 23:40:51 +02:00
|
|
|
case 'thumb-160x120':
|
|
|
|
$xformed_file = $this->executeThumbTransform($file, 160, 120);
|
|
|
|
break;
|
|
|
|
case 'thumb-60x45':
|
|
|
|
$xformed_file = $this->executeThumbTransform($file, 60, 45);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return new Aphront400Response();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$xformed_file) {
|
|
|
|
return new Aphront400Response();
|
|
|
|
}
|
|
|
|
|
|
|
|
$xform = new PhabricatorTransformedFile();
|
|
|
|
$xform->setOriginalPHID($this->phid);
|
|
|
|
$xform->setTransform($this->transform);
|
|
|
|
$xform->setTransformedPHID($xformed_file->getPHID());
|
|
|
|
$xform->save();
|
|
|
|
|
|
|
|
return $this->buildTransformedFileResponse($xform);
|
|
|
|
}
|
|
|
|
|
Support thumbnailing non-image files and straighten out setup for 'gd'
Summary:
Make 'gd' an explicit optional dependency, test for it in setup, and make the
software behave correctly if it is not available.
When generating file thumnails, provide reasonable defaults and behavior for
non-image files.
Test Plan:
Uploaded text files, pdf files, etc., and got real thumbnails instead of a
broken image.
Simulated setup and gd failures and walked through setup process and image
fallback for thumbnails.
Reviewed By: aran
Reviewers: toulouse, jungejason, tuomaspelkonen, aran
CC: aran, epriestley
Differential Revision: 446
2011-06-13 17:43:42 +02:00
|
|
|
private function buildDefaultTransformation(PhabricatorFile $file) {
|
|
|
|
static $regexps = array(
|
|
|
|
'@application/zip@' => 'zip',
|
|
|
|
'@image/@' => 'image',
|
|
|
|
'@application/pdf@' => 'pdf',
|
|
|
|
'@.*@' => 'default',
|
|
|
|
);
|
|
|
|
|
|
|
|
$type = $file->getMimeType();
|
|
|
|
$prefix = 'default';
|
|
|
|
foreach ($regexps as $regexp => $implied_prefix) {
|
|
|
|
if (preg_match($regexp, $type)) {
|
|
|
|
$prefix = $implied_prefix;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ($this->transform) {
|
|
|
|
case 'thumb-160x120':
|
|
|
|
$suffix = '160x120';
|
|
|
|
break;
|
|
|
|
case 'thumb-60x45':
|
|
|
|
$suffix = '60x45';
|
|
|
|
break;
|
|
|
|
default:
|
2014-06-09 20:36:49 +02:00
|
|
|
throw new Exception('Unsupported transformation type!');
|
Support thumbnailing non-image files and straighten out setup for 'gd'
Summary:
Make 'gd' an explicit optional dependency, test for it in setup, and make the
software behave correctly if it is not available.
When generating file thumnails, provide reasonable defaults and behavior for
non-image files.
Test Plan:
Uploaded text files, pdf files, etc., and got real thumbnails instead of a
broken image.
Simulated setup and gd failures and walked through setup process and image
fallback for thumbnails.
Reviewed By: aran
Reviewers: toulouse, jungejason, tuomaspelkonen, aran
CC: aran, epriestley
Differential Revision: 446
2011-06-13 17:43:42 +02:00
|
|
|
}
|
|
|
|
|
Use Celerity to version all static resources
Summary:
We don't use versioned URIs for images, so when they change users may get old versions.
This was a particular issue with the recent logo change, which several users reported cache-related issues from.
Instead, use Celerity to manage image URI versions in addition to CSS/JS.
This is complicated, because we need to rewrite image URIs inside of CSS, which means the hash of a CSS file has to be derived from the current image data. Otherwise, when we updated an image the CSS wouldn't update, so we wouldn't be any better off.
So basically we:
- Find all the "raw" files, and put them into the map.
- Find all the CSS/JS, perform content-altering transformations on it (i.e., not minification) based on the partial map, and then put it into the map based on transformed hashes.
(If we wanted, we could now do CSS variables or whatever for "free", more or less.)
Test Plan:
- Regenerated celerity map, browsed site, verified images generated with versioned URIs.
- Moved "blue" flag image over "green" flag image, regenerated map, verified "green" flag image and the associated CSS changed hashes.
- Added transformation unit tests; ran unit tests.
Reviewers: btrahan, vrana, jungejason
Reviewed By: vrana
CC: aran
Maniphest Tasks: T1073
Differential Revision: https://secure.phabricator.com/D2146
2012-04-08 19:07:51 +02:00
|
|
|
$path = celerity_get_resource_uri(
|
|
|
|
"/rsrc/image/icon/fatcow/thumbnails/{$prefix}{$suffix}.png");
|
|
|
|
|
Support thumbnailing non-image files and straighten out setup for 'gd'
Summary:
Make 'gd' an explicit optional dependency, test for it in setup, and make the
software behave correctly if it is not available.
When generating file thumnails, provide reasonable defaults and behavior for
non-image files.
Test Plan:
Uploaded text files, pdf files, etc., and got real thumbnails instead of a
broken image.
Simulated setup and gd failures and walked through setup process and image
fallback for thumbnails.
Reviewed By: aran
Reviewers: toulouse, jungejason, tuomaspelkonen, aran
CC: aran, epriestley
Differential Revision: 446
2011-06-13 17:43:42 +02:00
|
|
|
return id(new AphrontRedirectResponse())
|
|
|
|
->setURI($path);
|
|
|
|
}
|
|
|
|
|
2011-05-22 23:40:51 +02:00
|
|
|
private function buildTransformedFileResponse(
|
|
|
|
PhabricatorTransformedFile $xform) {
|
|
|
|
|
2013-09-30 18:38:13 +02:00
|
|
|
$file = id(new PhabricatorFileQuery())
|
2013-10-05 21:55:34 +02:00
|
|
|
->setViewer(PhabricatorUser::getOmnipotentUser())
|
2013-09-30 18:38:13 +02:00
|
|
|
->withPHIDs(array($xform->getTransformedPHID()))
|
|
|
|
->executeOne();
|
|
|
|
if (!$file) {
|
|
|
|
return new Aphront404Response();
|
2012-01-10 23:48:55 +01:00
|
|
|
}
|
|
|
|
|
2011-05-22 23:40:51 +02:00
|
|
|
// TODO: We could just delegate to the file view controller instead,
|
|
|
|
// which would save the client a roundtrip, but is slightly more complex.
|
2013-09-30 18:38:13 +02:00
|
|
|
$uri = $file->getBestURI();
|
2012-01-10 23:48:55 +01:00
|
|
|
return id(new AphrontRedirectResponse())->setURI($uri);
|
2011-05-22 23:40:51 +02:00
|
|
|
}
|
|
|
|
|
2012-10-08 22:26:10 +02:00
|
|
|
private function executePreviewTransform(PhabricatorFile $file, $size) {
|
|
|
|
$xformer = new PhabricatorImageTransformer();
|
|
|
|
return $xformer->executePreviewTransform($file, $size);
|
|
|
|
}
|
|
|
|
|
2011-05-22 23:40:51 +02:00
|
|
|
private function executeThumbTransform(PhabricatorFile $file, $x, $y) {
|
2011-06-26 17:37:47 +02:00
|
|
|
$xformer = new PhabricatorImageTransformer();
|
|
|
|
return $xformer->executeThumbTransform($file, $x, $y);
|
2011-05-22 23:40:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|