4 * Defines a storage engine which can write file data somewhere (like a
5 * database, local disk, Amazon S3, the A:\ drive, or a custom filer) and
8 * You can extend this class to provide new file storage backends.
10 * For more information, see @{article:File Storage Technical Documentation}.
12 * @task construct Constructing an Engine
13 * @task meta Engine Metadata
14 * @task file Managing File Data
15 * @task load Loading Storage Engines
17 abstract class PhabricatorFileStorageEngine
extends Phobject
{
19 const HMAC_INTEGRITY
= 'file.integrity';
22 * Construct a new storage engine.
26 final public function __construct() {
31 /* -( Engine Metadata )---------------------------------------------------- */
35 * Return a unique, nonempty string which identifies this storage engine.
36 * This is used to look up the storage engine when files needs to be read or
37 * deleted. For instance, if you store files by giving them to a duck for
38 * safe keeping in his nest down by the pond, you might return 'duck' from
41 * @return string Unique string for this engine, max length 32.
44 abstract public function getEngineIdentifier();
48 * Prioritize this engine relative to other engines.
50 * Engines with a smaller priority number get an opportunity to write files
51 * first. Generally, lower-latency filestores should have lower priority
52 * numbers, and higher-latency filestores should have higher priority
53 * numbers. Setting priority to approximately the number of milliseconds of
54 * read latency will generally produce reasonable results.
56 * In conjunction with filesize limits, the goal is to store small files like
57 * profile images, thumbnails, and text snippets in lower-latency engines,
58 * and store large files in higher-capacity engines.
60 * @return float Engine priority.
63 abstract public function getEnginePriority();
67 * Return `true` if the engine is currently writable.
69 * Engines that are disabled or missing configuration should return `false`
70 * to prevent new writes. If writes were made with this engine in the past,
71 * the application may still try to perform reads.
73 * @return bool True if this engine can support new writes.
76 abstract public function canWriteFiles();
80 * Return `true` if the engine has a filesize limit on storable files.
82 * The @{method:getFilesizeLimit} method can retrieve the actual limit. This
83 * method just removes the ambiguity around the meaning of a `0` limit.
85 * @return bool `true` if the engine has a filesize limit.
88 public function hasFilesizeLimit() {
94 * Return maximum storable file size, in bytes.
96 * Not all engines have a limit; use @{method:getFilesizeLimit} to check if
97 * an engine has a limit. Engines without a limit can store files of any
100 * By default, engines define a limit which supports chunked storage of
101 * large files. In most cases, you should not change this limit, even if an
102 * engine has vast storage capacity: chunked storage makes large files more
103 * manageable and enables features like resumable uploads.
105 * @return int Maximum storable file size, in bytes.
108 public function getFilesizeLimit() {
109 // NOTE: This 8MB limit is selected to be larger than the 4MB chunk size,
110 // but not much larger. Files between 0MB and 8MB will be stored normally;
111 // files larger than 8MB will be chunked.
112 return (1024 * 1024 * 8);
117 * Identifies storage engines that support unit tests.
119 * These engines are not used for production writes.
121 * @return bool True if this is a test engine.
124 public function isTestEngine() {
130 * Identifies chunking storage engines.
132 * If this is a storage engine which splits files into chunks and stores the
133 * chunks in other engines, it can return `true` to signal that other
134 * chunking engines should not try to store data here.
136 * @return bool True if this is a chunk engine.
139 public function isChunkEngine() {
144 /* -( Managing File Data )------------------------------------------------- */
148 * Write file data to the backing storage and return a handle which can later
149 * be used to read or delete it. For example, if the backing storage is local
150 * disk, the handle could be the path to the file.
152 * The caller will provide a $params array, which may be empty or may have
153 * some metadata keys (like "name" and "author") in it. You should be prepared
154 * to handle writes which specify no metadata, but might want to optionally
155 * use some keys in this array for debugging or logging purposes. This is
156 * the same dictionary passed to @{method:PhabricatorFile::newFromFileData},
157 * so you could conceivably do custom things with it.
159 * If you are unable to write for whatever reason (e.g., the disk is full),
160 * throw an exception. If there are other satisfactory but less-preferred
161 * storage engines available, they will be tried.
163 * @param string The file data to write.
164 * @param array File metadata (name, author), if available.
165 * @return string Unique string which identifies the stored file, max length
169 abstract public function writeFile($data, array $params);
173 * Read the contents of a file previously written by @{method:writeFile}.
175 * @param string The handle returned from @{method:writeFile} when the
177 * @return string File contents.
180 abstract public function readFile($handle);
184 * Delete the data for a file previously written by @{method:writeFile}.
186 * @param string The handle returned from @{method:writeFile} when the
191 abstract public function deleteFile($handle);
195 /* -( Loading Storage Engines )-------------------------------------------- */
199 * Select viable default storage engines according to configuration. We'll
200 * select the MySQL and Local Disk storage engines if they are configured
201 * to allow a given file.
203 * @param int File size in bytes.
206 public static function loadStorageEngines($length) {
207 $engines = self
::loadWritableEngines();
210 foreach ($engines as $key => $engine) {
211 if ($engine->hasFilesizeLimit()) {
212 $limit = $engine->getFilesizeLimit();
213 if ($limit < $length) {
218 $writable[$key] = $engine;
228 public static function loadAllEngines() {
229 return id(new PhutilClassMapQuery())
230 ->setAncestorClass(__CLASS__
)
231 ->setUniqueMethod('getEngineIdentifier')
232 ->setSortMethod('getEnginePriority')
240 private static function loadProductionEngines() {
241 $engines = self
::loadAllEngines();
244 foreach ($engines as $key => $engine) {
245 if ($engine->isTestEngine()) {
249 $active[$key] = $engine;
259 public static function loadWritableEngines() {
260 $engines = self
::loadProductionEngines();
263 foreach ($engines as $key => $engine) {
264 if (!$engine->canWriteFiles()) {
268 if ($engine->isChunkEngine()) {
269 // Don't select chunk engines as writable.
272 $writable[$key] = $engine;
281 public static function loadWritableChunkEngines() {
282 $engines = self
::loadProductionEngines();
285 foreach ($engines as $key => $engine) {
286 if (!$engine->canWriteFiles()) {
289 if (!$engine->isChunkEngine()) {
292 $chunk[$key] = $engine;
301 * Return the largest file size which can not be uploaded in chunks.
303 * Files smaller than this will always upload in one request, so clients
304 * can safely skip the allocation step.
306 * @return int|null Byte size, or `null` if there is no chunk support.
308 public static function getChunkThreshold() {
309 $engines = self
::loadWritableChunkEngines();
312 foreach ($engines as $engine) {
318 if ($min->getChunkSize() > $engine->getChunkSize()) {
319 $min = $engine->getChunkSize();
327 return $engine->getChunkSize();
330 public function getRawFileDataIterator(
331 PhabricatorFile
$file,
334 PhabricatorFileStorageFormat
$format) {
336 $formatted_data = $this->readFile($file->getStorageHandle());
338 $known_integrity = $file->getIntegrityHash();
339 if ($known_integrity !== null) {
340 $new_integrity = $this->newIntegrityHash($formatted_data, $format);
341 if (!phutil_hashes_are_identical($known_integrity, $new_integrity)) {
342 throw new PhabricatorFileIntegrityException(
344 'File data integrity check failed. Dark forces have corrupted '.
345 'or tampered with this file. The file data can not be read.'));
349 $formatted_data = array($formatted_data);
352 $format_iterator = $format->newReadIterator($formatted_data);
353 foreach ($format_iterator as $raw_chunk) {
357 if ($begin !== null && $end !== null) {
358 $data = substr($data, $begin, ($end - $begin));
359 } else if ($begin !== null) {
360 $data = substr($data, $begin);
361 } else if ($end !== null) {
362 $data = substr($data, 0, $end);
368 public function newIntegrityHash(
370 PhabricatorFileStorageFormat
$format) {
372 $hmac_name = self
::HMAC_INTEGRITY
;
374 $data_hash = PhabricatorHash
::digestWithNamedKey($data, $hmac_name);
375 $format_hash = $format->newFormatIntegrityHash();
377 $full_hash = "{$data_hash}/{$format_hash}";
379 return PhabricatorHash
::digestWithNamedKey($full_hash, $hmac_name);