2011-05-22 23:40:51 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2012-01-10 23:48:55 +01:00
|
|
|
* Copyright 2012 Facebook, Inc.
|
2011-05-22 23:40:51 +02:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
public function willProcessRequest(array $data) {
|
|
|
|
$this->transform = $data['transform'];
|
|
|
|
$this->phid = $data['phid'];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function processRequest() {
|
|
|
|
|
|
|
|
$xform = id(new PhabricatorTransformedFile())
|
|
|
|
->loadOneWhere(
|
|
|
|
'originalPHID = %s AND transform = %s',
|
|
|
|
$this->phid,
|
|
|
|
$this->transform);
|
|
|
|
|
|
|
|
if ($xform) {
|
|
|
|
return $this->buildTransformedFileResponse($xform);
|
|
|
|
}
|
|
|
|
|
|
|
|
$file = id(new PhabricatorFile())->loadOneWhere('phid = %s', $this->phid);
|
|
|
|
if (!$file) {
|
|
|
|
return new Aphront404Response();
|
|
|
|
}
|
|
|
|
|
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) {
|
2012-10-08 22:26:10 +02:00
|
|
|
case 'thumb-220x165':
|
|
|
|
$xformed_file = $this->executeThumbTransform($file, 220, 165);
|
|
|
|
break;
|
|
|
|
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:
|
|
|
|
throw new Exception("Unsupported transformation type!");
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
|
2012-01-10 23:48:55 +01:00
|
|
|
$file = id(new PhabricatorFile())->loadOneWhere(
|
|
|
|
'phid = %s',
|
|
|
|
$xform->getTransformedPHID());
|
|
|
|
if ($file) {
|
|
|
|
$uri = $file->getBestURI();
|
|
|
|
} else {
|
|
|
|
$bad_phid = $xform->getTransformedPHID();
|
|
|
|
throw new Exception(
|
|
|
|
"Unable to load file with phid {$bad_phid}."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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.
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|