Changeset View
Changeset View
Standalone View
Standalone View
src/applications/files/storage/PhabricatorFile.php
| Show First 20 Lines • Show All 209 Lines • ▼ Show 20 Lines | public static function buildFromFileDataOrHash( | ||||
| $file = id(new PhabricatorFile())->loadOneWhere( | $file = id(new PhabricatorFile())->loadOneWhere( | ||||
| 'name = %s AND contentHash = %s LIMIT 1', | 'name = %s AND contentHash = %s LIMIT 1', | ||||
| idx($params, 'name'), | idx($params, 'name'), | ||||
| self::hashFileContent($data)); | self::hashFileContent($data)); | ||||
| if (!$file) { | if (!$file) { | ||||
| $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites(); | $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites(); | ||||
| $file = PhabricatorFile::newFromFileData($data, $params); | $file = self::newFromFileData($data, $params); | ||||
| unset($unguarded); | unset($unguarded); | ||||
| } | } | ||||
| return $file; | return $file; | ||||
| } | } | ||||
| public static function newFileFromContentHash($hash, array $params) { | public static function newFileFromContentHash($hash, array $params) { | ||||
| // Check to see if a file with same contentHash exist | // Check to see if a file with same contentHash exist | ||||
| $file = id(new PhabricatorFile())->loadOneWhere( | $file = id(new PhabricatorFile())->loadOneWhere( | ||||
| 'contentHash = %s LIMIT 1', | 'contentHash = %s LIMIT 1', | ||||
| $hash); | $hash); | ||||
| if ($file) { | if ($file) { | ||||
| // copy storageEngine, storageHandle, storageFormat | // copy storageEngine, storageHandle, storageFormat | ||||
| $copy_of_storage_engine = $file->getStorageEngine(); | $copy_of_storage_engine = $file->getStorageEngine(); | ||||
| $copy_of_storage_handle = $file->getStorageHandle(); | $copy_of_storage_handle = $file->getStorageHandle(); | ||||
| $copy_of_storage_format = $file->getStorageFormat(); | $copy_of_storage_format = $file->getStorageFormat(); | ||||
| $copy_of_byte_size = $file->getByteSize(); | $copy_of_byte_size = $file->getByteSize(); | ||||
| $copy_of_mime_type = $file->getMimeType(); | $copy_of_mime_type = $file->getMimeType(); | ||||
| $new_file = PhabricatorFile::initializeNewFile(); | $new_file = self::initializeNewFile(); | ||||
| $new_file->setByteSize($copy_of_byte_size); | $new_file->setByteSize($copy_of_byte_size); | ||||
| $new_file->setContentHash($hash); | $new_file->setContentHash($hash); | ||||
| $new_file->setStorageEngine($copy_of_storage_engine); | $new_file->setStorageEngine($copy_of_storage_engine); | ||||
| $new_file->setStorageHandle($copy_of_storage_handle); | $new_file->setStorageHandle($copy_of_storage_handle); | ||||
| $new_file->setStorageFormat($copy_of_storage_format); | $new_file->setStorageFormat($copy_of_storage_format); | ||||
| $new_file->setMimeType($copy_of_mime_type); | $new_file->setMimeType($copy_of_mime_type); | ||||
| Show All 9 Lines | public static function newFileFromContentHash($hash, array $params) { | ||||
| return $file; | return $file; | ||||
| } | } | ||||
| public static function newChunkedFile( | public static function newChunkedFile( | ||||
| PhabricatorFileStorageEngine $engine, | PhabricatorFileStorageEngine $engine, | ||||
| $length, | $length, | ||||
| array $params) { | array $params) { | ||||
| $file = PhabricatorFile::initializeNewFile(); | $file = self::initializeNewFile(); | ||||
| $file->setByteSize($length); | $file->setByteSize($length); | ||||
| // TODO: We might be able to test the first chunk in order to figure | // TODO: We might be able to test the first chunk in order to figure | ||||
| // this out more reliably, since MIME detection usually examines headers. | // this out more reliably, since MIME detection usually examines headers. | ||||
| // However, enormous files are probably always either actually raw data | // However, enormous files are probably always either actually raw data | ||||
| // or reasonable to treat like raw data. | // or reasonable to treat like raw data. | ||||
| $file->setMimeType('application/octet-stream'); | $file->setMimeType('application/octet-stream'); | ||||
| Show All 37 Lines | if (isset($params['storageEngines'])) { | ||||
| } | } | ||||
| } | } | ||||
| assert_instances_of($engines, 'PhabricatorFileStorageEngine'); | assert_instances_of($engines, 'PhabricatorFileStorageEngine'); | ||||
| if (!$engines) { | if (!$engines) { | ||||
| throw new Exception(pht('No valid storage engines are available!')); | throw new Exception(pht('No valid storage engines are available!')); | ||||
| } | } | ||||
| $file = PhabricatorFile::initializeNewFile(); | $file = self::initializeNewFile(); | ||||
| $data_handle = null; | $data_handle = null; | ||||
| $engine_identifier = null; | $engine_identifier = null; | ||||
| $exceptions = array(); | $exceptions = array(); | ||||
| foreach ($engines as $engine) { | foreach ($engines as $engine) { | ||||
| $engine_class = get_class($engine); | $engine_class = get_class($engine); | ||||
| try { | try { | ||||
| list($engine_identifier, $data_handle) = $file->writeToEngine( | list($engine_identifier, $data_handle) = $file->writeToEngine( | ||||
| ▲ Show 20 Lines • Show All 684 Lines • ▼ Show 20 Lines | foreach ($names as $name) { | ||||
| $params = array( | $params = array( | ||||
| 'name' => $name, | 'name' => $name, | ||||
| 'ttl' => time() + (60 * 60 * 24 * 7), | 'ttl' => time() + (60 * 60 * 24 * 7), | ||||
| 'canCDN' => true, | 'canCDN' => true, | ||||
| 'builtin' => $name, | 'builtin' => $name, | ||||
| ); | ); | ||||
| $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites(); | $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites(); | ||||
| $file = PhabricatorFile::newFromFileData($data, $params); | $file = self::newFromFileData($data, $params); | ||||
| $xform = id(new PhabricatorTransformedFile()) | $xform = id(new PhabricatorTransformedFile()) | ||||
| ->setOriginalPHID(PhabricatorPHIDConstants::PHID_VOID) | ->setOriginalPHID(PhabricatorPHIDConstants::PHID_VOID) | ||||
| ->setTransform('builtin:'.$name) | ->setTransform('builtin:'.$name) | ||||
| ->setTransformedPHID($file->getPHID()) | ->setTransformedPHID($file->getPHID()) | ||||
| ->save(); | ->save(); | ||||
| unset($unguarded); | unset($unguarded); | ||||
| $file->attachObjectPHIDs(array()); | $file->attachObjectPHIDs(array()); | ||||
| ▲ Show 20 Lines • Show All 358 Lines • Show Last 20 Lines | |||||