Changeset View
Changeset View
Standalone View
Standalone View
src/applications/files/storage/PhabricatorFile.php
Show First 20 Lines • Show All 44 Lines • ▼ Show 20 Lines | final class PhabricatorFile extends PhabricatorFileDAO | ||||
protected $storageHandle; | protected $storageHandle; | ||||
protected $ttl; | protected $ttl; | ||||
protected $isExplicitUpload = 1; | protected $isExplicitUpload = 1; | ||||
protected $viewPolicy = PhabricatorPolicies::POLICY_USER; | protected $viewPolicy = PhabricatorPolicies::POLICY_USER; | ||||
private $objects = self::ATTACHABLE; | private $objects = self::ATTACHABLE; | ||||
private $objectPHIDs = self::ATTACHABLE; | private $objectPHIDs = self::ATTACHABLE; | ||||
private $originalFile = self::ATTACHABLE; | |||||
public static function initializeNewFile() { | |||||
return id(new PhabricatorFile()) | |||||
->attachOriginalFile(null) | |||||
->attachObjects(array()) | |||||
->attachObjectPHIDs(array()); | |||||
} | |||||
public function getConfiguration() { | public function getConfiguration() { | ||||
return array( | return array( | ||||
self::CONFIG_AUX_PHID => true, | self::CONFIG_AUX_PHID => true, | ||||
self::CONFIG_SERIALIZATION => array( | self::CONFIG_SERIALIZATION => array( | ||||
'metadata' => self::SERIALIZATION_JSON, | 'metadata' => self::SERIALIZATION_JSON, | ||||
), | ), | ||||
) + parent::getConfiguration(); | ) + parent::getConfiguration(); | ||||
▲ Show 20 Lines • Show All 124 Lines • ▼ Show 20 Lines | public static function newFileFromContentHash($hash, array $params) { | ||||
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_byteSize = $file->getByteSize(); | $copy_of_byteSize = $file->getByteSize(); | ||||
$copy_of_mimeType = $file->getMimeType(); | $copy_of_mimeType = $file->getMimeType(); | ||||
$new_file = new PhabricatorFile(); | $new_file = PhabricatorFile::initializeNewFile(); | ||||
$new_file->setByteSize($copy_of_byteSize); | $new_file->setByteSize($copy_of_byteSize); | ||||
$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_mimeType); | $new_file->setMimeType($copy_of_mimeType); | ||||
Show All 19 Lines | if (isset($params['storageEngines'])) { | ||||
$engines = $selector->selectStorageEngines($data, $params); | $engines = $selector->selectStorageEngines($data, $params); | ||||
} | } | ||||
assert_instances_of($engines, 'PhabricatorFileStorageEngine'); | assert_instances_of($engines, 'PhabricatorFileStorageEngine'); | ||||
if (!$engines) { | if (!$engines) { | ||||
throw new Exception('No valid storage engines are available!'); | throw new Exception('No valid storage engines are available!'); | ||||
} | } | ||||
$file = new PhabricatorFile(); | $file = PhabricatorFile::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 605 Lines • ▼ Show 20 Lines | public function getObjectPHIDs() { | ||||
return $this->assertAttached($this->objectPHIDs); | return $this->assertAttached($this->objectPHIDs); | ||||
} | } | ||||
public function attachObjectPHIDs(array $object_phids) { | public function attachObjectPHIDs(array $object_phids) { | ||||
$this->objectPHIDs = $object_phids; | $this->objectPHIDs = $object_phids; | ||||
return $this; | return $this; | ||||
} | } | ||||
public function getOriginalFile() { | |||||
return $this->assertAttached($this->originalFile); | |||||
} | |||||
public function attachOriginalFile(PhabricatorFile $file = null) { | |||||
$this->originalFile = $file; | |||||
return $this; | |||||
} | |||||
public function getImageHeight() { | public function getImageHeight() { | ||||
if (!$this->isViewableImage()) { | if (!$this->isViewableImage()) { | ||||
return null; | return null; | ||||
} | } | ||||
return idx($this->metadata, self::METADATA_IMAGE_HEIGHT); | return idx($this->metadata, self::METADATA_IMAGE_HEIGHT); | ||||
} | } | ||||
public function getImageWidth() { | public function getImageWidth() { | ||||
▲ Show 20 Lines • Show All 79 Lines • ▼ Show 20 Lines | id(new PhabricatorEdgeEditor()) | ||||
->addEdge($phid, $edge_type, $this->getPHID()) | ->addEdge($phid, $edge_type, $this->getPHID()) | ||||
->save(); | ->save(); | ||||
return $this; | return $this; | ||||
} | } | ||||
/** | /** | ||||
* Remove the policy edge between this file and some object. | |||||
* | |||||
* @param phid Object PHID to detach from. | |||||
* @return this | |||||
*/ | |||||
public function detachFromObject($phid) { | |||||
$edge_type = PhabricatorEdgeConfig::TYPE_OBJECT_HAS_FILE; | |||||
id(new PhabricatorEdgeEditor()) | |||||
->removeEdge($phid, $edge_type, $this->getPHID()) | |||||
->save(); | |||||
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. | ||||
* | * | ||||
* @param map<string, wild> Bag of parameters, see @{class:PhabricatorFile} | * @param map<string, wild> Bag of parameters, see @{class:PhabricatorFile} | ||||
* for documentation. | * for documentation. | ||||
* @return this | * @return this | ||||
▲ Show 20 Lines • Show All 74 Lines • ▼ Show 20 Lines | public function hasAutomaticCapability($capability, PhabricatorUser $viewer) { | ||||
if ($viewer_phid) { | if ($viewer_phid) { | ||||
if ($this->getAuthorPHID() == $viewer_phid) { | if ($this->getAuthorPHID() == $viewer_phid) { | ||||
return true; | return true; | ||||
} | } | ||||
} | } | ||||
switch ($capability) { | switch ($capability) { | ||||
case PhabricatorPolicyCapability::CAN_VIEW: | case PhabricatorPolicyCapability::CAN_VIEW: | ||||
// If you can see the file this file is a transform of, you can see | |||||
// this file. | |||||
if ($this->getOriginalFile()) { | |||||
return true; | |||||
} | |||||
// If you can see any object this file is attached to, you can see | // If you can see any object this file is attached to, you can see | ||||
// the file. | // the file. | ||||
return (count($this->getObjects()) > 0); | return (count($this->getObjects()) > 0); | ||||
} | } | ||||
return false; | return false; | ||||
} | } | ||||
public function describeAutomaticCapability($capability) { | public function describeAutomaticCapability($capability) { | ||||
$out = array(); | $out = array(); | ||||
$out[] = pht('The user who uploaded a file can always view and edit it.'); | $out[] = pht('The user who uploaded a file can always view and edit it.'); | ||||
switch ($capability) { | switch ($capability) { | ||||
case PhabricatorPolicyCapability::CAN_VIEW: | case PhabricatorPolicyCapability::CAN_VIEW: | ||||
$out[] = pht( | $out[] = pht( | ||||
'Files attached to objects are visible to users who can view '. | 'Files attached to objects are visible to users who can view '. | ||||
'those objects.'); | 'those objects.'); | ||||
$out[] = pht( | |||||
'Thumbnails are visible only to users who can view the original '. | |||||
'file.'); | |||||
break; | break; | ||||
} | } | ||||
return $out; | return $out; | ||||
} | } | ||||
/* -( PhabricatorSubscribableInterface Implementation )-------------------- */ | /* -( PhabricatorSubscribableInterface Implementation )-------------------- */ | ||||
Show All 37 Lines |