Changeset View
Changeset View
Standalone View
Standalone View
src/applications/files/storage/PhabricatorFile.php
Show First 20 Lines • Show All 1,408 Lines • ▼ Show 20 Lines | public function newIntegrityHash() { | ||||
$storage_handle = $this->getStorageHandle(); | $storage_handle = $this->getStorageHandle(); | ||||
$data = $engine->readFile($storage_handle); | $data = $engine->readFile($storage_handle); | ||||
return $engine->newIntegrityHash($data, $format); | return $engine->newIntegrityHash($data, $format); | ||||
} | } | ||||
/** | /** | ||||
* Write the policy edge between this file and some object. | * Write the policy edge between this file and some object. | ||||
* This method is successful even if the file is already attached. | |||||
* | * | ||||
* @param phid Object PHID to attach to. | * @param phid Object PHID to attach to. | ||||
* @return this | * @return this | ||||
*/ | */ | ||||
public function attachToObject($phid) { | public function attachToObject($phid) { | ||||
self::attachFileToObject($this->getPHID(), $phid); | |||||
return $this; | |||||
} | |||||
/** | |||||
* Write the policy edge between a file and some object. | |||||
* This method is successful even if the file is already attached. | |||||
* NOTE: Please avoid to use this static method directly. | |||||
* Instead, use PhabricatorFile#attachToObject(phid). | |||||
* | |||||
* @param phid File PHID to attach from. | |||||
* @param phid Object PHID to attach to. | |||||
* @return void | |||||
*/ | |||||
public static function attachFileToObject($file_phid, $object_phid) { | |||||
$attachment_table = new PhabricatorFileAttachment(); | $attachment_table = new PhabricatorFileAttachment(); | ||||
avivey: For extra safety, let's add a check for `phid_get_type($file_phid) === PHID_TYPE_FILE` here. | |||||
$attachment_conn = $attachment_table->establishConnection('w'); | $attachment_conn = $attachment_table->establishConnection('w'); | ||||
queryfx( | queryfx( | ||||
$attachment_conn, | $attachment_conn, | ||||
'INSERT INTO %R (objectPHID, filePHID, attachmentMode, | 'INSERT INTO %R (objectPHID, filePHID, attachmentMode, | ||||
attacherPHID, dateCreated, dateModified) | attacherPHID, dateCreated, dateModified) | ||||
VALUES (%s, %s, %s, %ns, %d, %d) | VALUES (%s, %s, %s, %ns, %d, %d) | ||||
ON DUPLICATE KEY UPDATE | ON DUPLICATE KEY UPDATE | ||||
attachmentMode = VALUES(attachmentMode), | attachmentMode = VALUES(attachmentMode), | ||||
attacherPHID = VALUES(attacherPHID), | attacherPHID = VALUES(attacherPHID), | ||||
dateModified = VALUES(dateModified)', | dateModified = VALUES(dateModified)', | ||||
$attachment_table, | $attachment_table, | ||||
$phid, | $object_phid, | ||||
$this->getPHID(), | $file_phid, | ||||
PhabricatorFileAttachment::MODE_ATTACH, | PhabricatorFileAttachment::MODE_ATTACH, | ||||
null, | null, | ||||
PhabricatorTime::getNow(), | PhabricatorTime::getNow(), | ||||
PhabricatorTime::getNow()); | PhabricatorTime::getNow()); | ||||
return $this; | |||||
} | } | ||||
/** | /** | ||||
* Configure a newly created file object according to specified parameters. | * Configure a newly created file object according to specified parameters. | ||||
* | * | ||||
* This method is called both when creating a file from fresh data, and | * This method is called both when creating a file from fresh data, and | ||||
* when creating a new file which reuses existing storage. | * when creating a new file which reuses existing storage. | ||||
▲ Show 20 Lines • Show All 317 Lines • Show Last 20 Lines |
Content licensed under Creative Commons Attribution-ShareAlike 4.0 (CC-BY-SA) unless otherwise noted; code licensed under Apache 2.0 or other open source licenses. · CC BY-SA 4.0 · Apache 2.0
For extra safety, let's add a check for phid_get_type($file_phid) === PHID_TYPE_FILE here.