Changeset View
Changeset View
Standalone View
Standalone View
src/applications/files/storage/PhabricatorFile.php
Show First 20 Lines • Show All 192 Lines • ▼ Show 20 Lines | final class PhabricatorFile extends PhabricatorFileDAO | ||||
} | } | ||||
public static function newFromXHRUpload($data, array $params = array()) { | public static function newFromXHRUpload($data, array $params = array()) { | ||||
return self::newFromFileData($data, $params); | return self::newFromFileData($data, $params); | ||||
} | } | ||||
public static function newFileFromContentHash($hash, array $params) { | public static function newFileFromContentHash($hash, array $params) { | ||||
// Check to see if a file with same contentHash exist | if ($hash === null) { | ||||
return null; | |||||
} | |||||
// Check to see if a file with same hash already exists. | |||||
$file = id(new PhabricatorFile())->loadOneWhere( | $file = id(new PhabricatorFile())->loadOneWhere( | ||||
'contentHash = %s LIMIT 1', | 'contentHash = %s LIMIT 1', | ||||
$hash); | $hash); | ||||
if (!$file) { | |||||
return null; | |||||
} | |||||
if ($file) { | |||||
$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_storage_properties = $file->getStorageProperties(); | $copy_of_storage_properties = $file->getStorageProperties(); | ||||
$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 = self::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->setStorageProperties($copy_of_storage_properties); | $new_file->setStorageProperties($copy_of_storage_properties); | ||||
$new_file->setMimeType($copy_of_mime_type); | $new_file->setMimeType($copy_of_mime_type); | ||||
$new_file->copyDimensions($file); | $new_file->copyDimensions($file); | ||||
$new_file->readPropertiesFromParameters($params); | $new_file->readPropertiesFromParameters($params); | ||||
$new_file->save(); | $new_file->save(); | ||||
return $new_file; | return $new_file; | ||||
} | } | ||||
return $file; | |||||
} | |||||
public static function newChunkedFile( | public static function newChunkedFile( | ||||
PhabricatorFileStorageEngine $engine, | PhabricatorFileStorageEngine $engine, | ||||
$length, | $length, | ||||
array $params) { | array $params) { | ||||
$file = self::initializeNewFile(); | $file = self::initializeNewFile(); | ||||
$file->setByteSize($length); | $file->setByteSize($length); | ||||
▲ Show 20 Lines • Show All 104 Lines • ▼ Show 20 Lines | private static function buildFromFileData($data, array $params = array()) { | ||||
if (!$data_handle) { | if (!$data_handle) { | ||||
throw new PhutilAggregateException( | throw new PhutilAggregateException( | ||||
pht('All storage engines failed to write file:'), | pht('All storage engines failed to write file:'), | ||||
$exceptions); | $exceptions); | ||||
} | } | ||||
$file->setByteSize(strlen($data)); | $file->setByteSize(strlen($data)); | ||||
$file->setContentHash(self::hashFileContent($data)); | |||||
$hash = self::hashFileContent($data); | |||||
$file->setContentHash($hash); | |||||
$file->setStorageEngine($engine_identifier); | $file->setStorageEngine($engine_identifier); | ||||
$file->setStorageHandle($data_handle); | $file->setStorageHandle($data_handle); | ||||
$file->readPropertiesFromParameters($params); | $file->readPropertiesFromParameters($params); | ||||
if (!$file->getMimeType()) { | if (!$file->getMimeType()) { | ||||
$tmp = new TempFile(); | $tmp = new TempFile(); | ||||
Show All 9 Lines | private static function buildFromFileData($data, array $params = array()) { | ||||
$file->save(); | $file->save(); | ||||
return $file; | return $file; | ||||
} | } | ||||
public static function newFromFileData($data, array $params = array()) { | public static function newFromFileData($data, array $params = array()) { | ||||
$hash = self::hashFileContent($data); | $hash = self::hashFileContent($data); | ||||
$file = self::newFileFromContentHash($hash, $params); | |||||
if ($hash !== null) { | |||||
$file = self::newFileFromContentHash($hash, $params); | |||||
if ($file) { | if ($file) { | ||||
return $file; | return $file; | ||||
} | } | ||||
} | |||||
return self::buildFromFileData($data, $params); | return self::buildFromFileData($data, $params); | ||||
} | } | ||||
public function migrateToEngine( | public function migrateToEngine( | ||||
PhabricatorFileStorageEngine $engine, | PhabricatorFileStorageEngine $engine, | ||||
$make_copy) { | $make_copy) { | ||||
▲ Show 20 Lines • Show All 310 Lines • ▼ Show 20 Lines | if (!$usage) { | ||||
} catch (Exception $ex) { | } catch (Exception $ex) { | ||||
// In the worst case, we're leaving some data stranded in a storage | // In the worst case, we're leaving some data stranded in a storage | ||||
// engine, which is not a big deal. | // engine, which is not a big deal. | ||||
phlog($ex); | phlog($ex); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
public static function hashFileContent($data) { | public static function hashFileContent($data) { | ||||
return sha1($data); | return null; | ||||
} | } | ||||
public function loadFileData() { | public function loadFileData() { | ||||
$iterator = $this->getFileDataIterator(); | $iterator = $this->getFileDataIterator(); | ||||
return $this->loadDataFromIterator($iterator); | return $this->loadDataFromIterator($iterator); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 801 Lines • Show Last 20 Lines |