Changeset View
Changeset View
Standalone View
Standalone View
src/applications/files/storage/PhabricatorFile.php
| Show First 20 Lines • Show All 312 Lines • ▼ Show 20 Lines | public function migrateToEngine(PhabricatorFileStorageEngine $engine) { | ||||
| ); | ); | ||||
| list($new_identifier, $new_handle) = $this->writeToEngine( | list($new_identifier, $new_handle) = $this->writeToEngine( | ||||
| $engine, | $engine, | ||||
| $data, | $data, | ||||
| $params); | $params); | ||||
| $old_engine = $this->instantiateStorageEngine(); | $old_engine = $this->instantiateStorageEngine(); | ||||
| $old_identifier = $this->getStorageEngine(); | |||||
| $old_handle = $this->getStorageHandle(); | $old_handle = $this->getStorageHandle(); | ||||
| $this->setStorageEngine($new_identifier); | $this->setStorageEngine($new_identifier); | ||||
| $this->setStorageHandle($new_handle); | $this->setStorageHandle($new_handle); | ||||
| $this->save(); | $this->save(); | ||||
| $old_engine->deleteFile($old_handle); | $this->deleteFileDataIfUnused( | ||||
| $old_engine, | |||||
| $old_identifier, | |||||
| $old_handle); | |||||
| return $this; | return $this; | ||||
| } | } | ||||
| private function writeToEngine( | private function writeToEngine( | ||||
| PhabricatorFileStorageEngine $engine, | PhabricatorFileStorageEngine $engine, | ||||
| $data, | $data, | ||||
| array $params) { | array $params) { | ||||
| ▲ Show 20 Lines • Show All 97 Lines • ▼ Show 20 Lines | public function delete() { | ||||
| $this->openTransaction(); | $this->openTransaction(); | ||||
| foreach ($inbound_xforms as $inbound_xform) { | foreach ($inbound_xforms as $inbound_xform) { | ||||
| $inbound_xform->delete(); | $inbound_xform->delete(); | ||||
| } | } | ||||
| $ret = parent::delete(); | $ret = parent::delete(); | ||||
| $this->saveTransaction(); | $this->saveTransaction(); | ||||
| // Check to see if other files are using storage | $this->deleteFileDataIfUnused( | ||||
| $other_file = id(new PhabricatorFile())->loadAllWhere( | $this->instantiateStorageEngine(), | ||||
| 'storageEngine = %s AND storageHandle = %s AND | |||||
| storageFormat = %s AND id != %d LIMIT 1', | |||||
| $this->getStorageEngine(), | $this->getStorageEngine(), | ||||
| $this->getStorageHandle(), | $this->getStorageHandle()); | ||||
| $this->getStorageFormat(), | |||||
| $this->getID()); | |||||
| // If this is the only file using the storage, delete storage | return $ret; | ||||
| if (!$other_file) { | } | ||||
| $engine = $this->instantiateStorageEngine(); | |||||
| /** | |||||
| * Destroy stored file data if there are no remaining files which reference | |||||
| * it. | |||||
| */ | |||||
| private function deleteFileDataIfUnused( | |||||
| PhabricatorFileStorageEngine $engine, | |||||
| $engine_identifier, | |||||
| $handle) { | |||||
| // Check to see if any files are using storage. | |||||
| $usage = id(new PhabricatorFile())->loadAllWhere( | |||||
| 'storageEngine = %s AND storageHandle = %s LIMIT 1', | |||||
| $engine_identifier, | |||||
| $handle); | |||||
| // If there are no files using the storage, destroy the actual storage. | |||||
| if (!$usage) { | |||||
| try { | try { | ||||
| $engine->deleteFile($this->getStorageHandle()); | $engine->deleteFile($handle); | ||||
| } 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 fine. | // engine, which is not a big deal. | ||||
| phlog($ex); | phlog($ex); | ||||
| } | } | ||||
| } | } | ||||
| return $ret; | |||||
| } | } | ||||
| public static function hashFileContent($data) { | public static function hashFileContent($data) { | ||||
| return sha1($data); | return sha1($data); | ||||
| } | } | ||||
| public function loadFileData() { | public function loadFileData() { | ||||
| $engine = $this->instantiateStorageEngine(); | $engine = $this->instantiateStorageEngine(); | ||||
| $data = $engine->readFile($this->getStorageHandle()); | $data = $engine->readFile($this->getStorageHandle()); | ||||
| ▲ Show 20 Lines • Show All 607 Lines • Show Last 20 Lines | |||||