private function buildDefaultTransformation(PhabricatorFile $file) { static $regexps = array('@application/zip@' => 'zip', '@image/@' => 'image', '@application/pdf@' => 'pdf', '@.*@' => 'default'); $type = $file->getMimeType(); $prefix = 'default'; foreach ($regexps as $regexp => $implied_prefix) { if (preg_match($regexp, $type)) { $prefix = $implied_prefix; break; } } switch ($this->transform) { case 'thumb-280x210': $suffix = '280x210'; break; case 'thumb-160x120': $suffix = '160x120'; break; case 'thumb-60x45': $suffix = '60x45'; break; case 'preview-100': $suffix = '.p100'; break; default: throw new Exception('Unsupported transformation type!'); } $path = celerity_get_resource_uri("rsrc/image/icon/fatcow/thumbnails/{$prefix}{$suffix}.png"); return id(new AphrontRedirectResponse())->setURI($path); }
private function buildActionView(PhabricatorUser $viewer, PhabricatorPaste $paste, PhabricatorFile $file) { $can_edit = PhabricatorPolicyFilter::hasCapability($viewer, $paste, PhabricatorPolicyCapability::CAN_EDIT); $can_fork = $viewer->isLoggedIn(); $fork_uri = $this->getApplicationURI('/create/?parent=' . $paste->getID()); return id(new PhabricatorActionListView())->setUser($viewer)->setObject($paste)->setObjectURI($this->getRequest()->getRequestURI())->addAction(id(new PhabricatorActionView())->setName(pht('Edit Paste'))->setIcon('fa-pencil')->setDisabled(!$can_edit)->setWorkflow(!$can_edit)->setHref($this->getApplicationURI('/edit/' . $paste->getID() . '/')))->addAction(id(new PhabricatorActionView())->setName(pht('Fork This Paste'))->setIcon('fa-code-fork')->setDisabled(!$can_fork)->setWorkflow(!$can_fork)->setHref($fork_uri))->addAction(id(new PhabricatorActionView())->setName(pht('View Raw File'))->setIcon('fa-file-text-o')->setHref($file->getBestURI())); }
/** * Calculate the DiffMatchPatch patch between two Phabricator files. * * @phutil-external-symbol class diff_match_patch */ public static function calculatePatch(PhabricatorFile $old = null, PhabricatorFile $new = null) { $root = dirname(phutil_get_library_root('phabricator')); require_once $root . '/externals/diff_match_patch/diff_match_patch.php'; $old_hash = self::EMPTY_HASH; $new_hash = self::EMPTY_HASH; if ($old !== null) { $old_hash = $old->getContentHash(); } if ($new !== null) { $new_hash = $new->getContentHash(); } $old_content = ''; $new_content = ''; if ($old_hash === $new_hash) { return null; } if ($old_hash !== self::EMPTY_HASH) { $old_content = $old->loadFileData(); } else { $old_content = ''; } if ($new_hash !== self::EMPTY_HASH) { $new_content = $new->loadFileData(); } else { $new_content = ''; } $dmp = new diff_match_patch(); $dmp_patches = $dmp->patch_make($old_content, $new_content); return $dmp->patch_toText($dmp_patches); }
/** * Very crudely scale an image up or down to an exact size. */ private function crudelyScaleTo(PhabricatorFile $file, $dx, $dy) { $data = $file->loadFileData(); $src = imagecreatefromstring($data); $dst = $this->applyScaleTo($src, $dx, $dy); return $this->saveImageDataInAnyFormat($dst, $file->getMimeType()); }
private function newChunkQuery(PhabricatorUser $viewer, PhabricatorFile $file) { $engine = $file->instantiateStorageEngine(); if (!$engine->isChunkEngine()) { throw new Exception(pht('File "%s" does not have chunks!', $file->getPHID())); } return id(new PhabricatorFileChunkQuery())->setViewer($viewer)->withChunkHandles(array($file->getStorageHandle())); }
private function buildSourceCodeView(PhabricatorPaste $paste, PhabricatorFile $file) { $language = $paste->getLanguage(); $source = $file->loadFileData(); if (empty($language)) { $source = PhabricatorSyntaxHighlighter::highlightWithFilename($paste->getTitle(), $source); } else { $source = PhabricatorSyntaxHighlighter::highlightWithLanguage($language, $source); } $lines = explode("\n", $source); return id(new PhabricatorSourceCodeView())->setLines($lines); }
private function renderCommentForm(PhabricatorFile $file) { $viewer = $this->getViewer(); if (!$viewer->isLoggedIn()) { $login_href = id(new PhutilURI('/auth/start/'))->setQueryParam('next', '/' . $file->getMonogram()); return id(new PHUIFormLayoutView())->addClass('phui-comment-panel-empty')->appendChild(id(new PHUIButtonView())->setTag('a')->setText(pht('Login to Comment'))->setHref((string) $login_href)); } $draft = PhabricatorDraft::newFromUserAndKey($viewer, $file->getPHID()); $post_uri = $this->getApplicationURI('thread/' . $file->getPHID() . '/'); $form = id(new AphrontFormView())->setUser($viewer)->setAction($post_uri)->addSigil('lightbox-comment-form')->addClass('lightbox-comment-form')->setWorkflow(true)->appendChild(id(new PhabricatorRemarkupControl())->setUser($viewer)->setName('comment')->setValue($draft->getDraft()))->appendChild(id(new AphrontFormSubmitControl())->setValue(pht('Comment'))); $view = phutil_tag_div('phui-comment-panel', $form); return $view; }
public function processRequest() { if (!PhabricatorEnv::getEnvConfig('files.enable-proxy')) { return new Aphront400Response(); } $request = $this->getRequest(); $uri = $request->getStr('uri'); $proxy = id(new PhabricatorFileProxyImage())->loadOneWhere('uri = %s', $uri); if (!$proxy) { // This write is fine to skip CSRF checks for, we're just building a // cache of some remote image. $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites(); $file = PhabricatorFile::newFromFileDownload($uri, nonempty(basename($uri), 'proxied-file')); if ($file) { $proxy = new PhabricatorFileProxyImage(); $proxy->setURI($uri); $proxy->setFilePHID($file->getPHID()); $proxy->save(); } unset($unguarded); } if ($proxy) { $file = id(new PhabricatorFile())->loadOneWhere('phid = %s', $proxy->getFilePHID()); if ($file) { $view_uri = $file->getBestURI(); } else { $bad_phid = $proxy->getFilePHID(); throw new Exception("Unable to load file with phid {$bad_phid}."); } return id(new AphrontRedirectResponse())->setURI($view_uri); } return new Aphront400Response(); }
public function render() { $viewer = $this->getViewer(); if (!$viewer->isLoggedIn()) { return null; } $instructions_id = 'phabricator-global-drag-and-drop-upload-instructions'; require_celerity_resource('global-drag-and-drop-css'); $hint_text = $this->getHintText(); if (!strlen($hint_text)) { $hint_text = "⇪ " . pht('Drop Files to Upload'); } // Use the configured default view policy. Drag and drop uploads use // a more restrictive view policy if we don't specify a policy explicitly, // as the more restrictive policy is correct for most drop targets (like // Pholio uploads and Remarkup text areas). $view_policy = $this->getViewPolicy(); if ($view_policy === null) { $view_policy = PhabricatorFile::initializeNewFile()->getViewPolicy(); } $submit_uri = $this->getSubmitURI(); $done_uri = '/file/query/authored/'; Javelin::initBehavior('global-drag-and-drop', array('ifSupported' => $this->showIfSupportedID, 'instructions' => $instructions_id, 'uploadURI' => '/file/dropupload/', 'submitURI' => $submit_uri, 'browseURI' => $done_uri, 'viewPolicy' => $view_policy, 'chunkThreshold' => PhabricatorFileStorageEngine::getChunkThreshold())); return phutil_tag('div', array('id' => $instructions_id, 'class' => 'phabricator-global-upload-instructions', 'style' => 'display: none;'), $hint_text); }
public function readRequest(PhabricatorConfigOption $option, AphrontRequest $request) { $viewer = $request->getViewer(); $view_policy = PhabricatorPolicies::POLICY_PUBLIC; if ($request->getBool('removeLogo')) { $logo_image_phid = null; } else { if ($request->getFileExists('logoImage')) { $logo_image = PhabricatorFile::newFromPHPUpload(idx($_FILES, 'logoImage'), array('name' => 'logo', 'authorPHID' => $viewer->getPHID(), 'viewPolicy' => $view_policy, 'canCDN' => true, 'isExplicitUpload' => true)); $logo_image_phid = $logo_image->getPHID(); } else { $logo_image_phid = self::getLogoImagePHID(); } } $wordmark_text = $request->getStr('wordmarkText'); $value = array('logoImagePHID' => $logo_image_phid, 'wordmarkText' => $wordmark_text); $errors = array(); $e_value = null; try { $this->validateOption($option, $value); } catch (Exception $ex) { $e_value = pht('Invalid'); $errors[] = $ex->getMessage(); $value = array(); } return array($e_value, $errors, $value, phutil_json_encode($value)); }
protected function execute(ConduitAPIRequest $request) { $viewer = $request->getUser(); $file_phid = $request->getValue('filePHID'); $file = $this->loadFileByPHID($viewer, $file_phid); $start = $request->getValue('byteStart'); $data = $request->getValue('data'); $encoding = $request->getValue('dataEncoding'); switch ($encoding) { case 'base64': $data = $this->decodeBase64($data); break; case null: break; default: throw new Exception(pht('Unsupported data encoding.')); } $length = strlen($data); $chunk = $this->loadFileChunkForUpload($viewer, $file, $start, $start + $length); // NOTE: These files have a view policy which prevents normal access. They // are only accessed through the storage engine. $chunk_data = PhabricatorFile::newFromFileData($data, array('name' => $file->getMonogram() . '.chunk-' . $chunk->getID(), 'viewPolicy' => PhabricatorPolicies::POLICY_NOONE)); $chunk->setDataFilePHID($chunk_data->getPHID())->save(); $missing = $this->loadAnyMissingChunk($viewer, $file); if (!$missing) { $file->setIsPartial(0)->save(); } return null; }
public function processRequest() { if ($this->id) { $macro = id(new PhabricatorFileImageMacro())->load($this->id); if (!$macro) { return new Aphront404Response(); } } else { $macro = new PhabricatorFileImageMacro(); } $errors = array(); $e_name = true; $request = $this->getRequest(); $user = $request->getUser(); if ($request->isFormPost()) { $macro->setName($request->getStr('name')); if (!strlen($macro->getName())) { $errors[] = 'Macro name is required.'; $e_name = 'Required'; } else { if (!preg_match('/^[a-z0-9_-]{3,}$/', $macro->getName())) { $errors[] = 'Macro must be at least three characters long and contain ' . 'only lowercase letters, digits, hyphen and underscore.'; $e_name = 'Invalid'; } else { $e_name = null; } } if (!$errors) { $file = PhabricatorFile::newFromPHPUpload(idx($_FILES, 'file'), array('name' => $request->getStr('name'), 'authorPHID' => $user->getPHID())); $macro->setFilePHID($file->getPHID()); try { $macro->save(); return id(new AphrontRedirectResponse())->setURI('/file/macro/'); } catch (AphrontQueryDuplicateKeyException $ex) { $errors[] = 'Macro name is not unique!'; $e_name = 'Duplicate'; } } } if ($errors) { $error_view = new AphrontErrorView(); $error_view->setTitle('Form Errors'); $error_view->setErrors($errors); } else { $error_view = null; } $form = new AphrontFormView(); $form->setAction('/file/macro/edit/'); $form->setUser($request->getUser()); $form->setEncType('multipart/form-data')->appendChild(id(new AphrontFormTextControl())->setLabel('Name')->setName('name')->setValue($macro->getName())->setCaption('This word or phrase will be replaced with the image.')->setError($e_name))->appendChild(id(new AphrontFormFileControl())->setLabel('File')->setName('file')->setError(true))->appendChild(id(new AphrontFormSubmitControl())->setValue('Save Image Macro')->addCancelButton('/file/macro/')); $panel = new AphrontPanelView(); if ($macro->getID()) { $panel->setHeader('Edit Image Macro'); } else { $panel->setHeader('Create Image Macro'); } $panel->appendChild($form); $panel->setWidth(AphrontPanelView::WIDTH_FORM); return $this->buildStandardPageResponse(array($error_view, $panel), array('title' => 'Edit Image Macro')); }
public function processRequest() { // No CSRF for SendGrid. $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites(); $request = $this->getRequest(); $user = $request->getUser(); $raw_headers = $request->getStr('headers'); $raw_headers = explode("\n", rtrim($raw_headers)); $raw_dict = array(); foreach (array_filter($raw_headers) as $header) { list($name, $value) = explode(':', $header, 2); $raw_dict[$name] = ltrim($value); } $headers = array('to' => $request->getStr('to'), 'from' => $request->getStr('from'), 'subject' => $request->getStr('subject')) + $raw_dict; $received = new PhabricatorMetaMTAReceivedMail(); $received->setHeaders($headers); $received->setBodies(array('text' => $request->getStr('text'), 'html' => $request->getStr('from'))); $file_phids = array(); foreach ($_FILES as $file_raw) { try { $file = PhabricatorFile::newFromPHPUpload($file_raw, array('authorPHID' => $user->getPHID())); $file_phids[] = $file->getPHID(); } catch (Exception $ex) { phlog($ex); } } $received->setAttachments($file_phids); $received->save(); $received->processReceivedMail(); $response = new AphrontWebpageResponse(); $response->setContent("Got it! Thanks, SendGrid!\n"); return $response; }
public function handleRequest(AphrontRequest $request) { $viewer = $this->getViewer(); $key = $this->newKeyForObjectPHID($request->getStr('objectPHID')); if (!$key) { return new Aphront404Response(); } $cancel_uri = $key->getObject()->getSSHPublicKeyManagementURI($viewer); $token = id(new PhabricatorAuthSessionEngine())->requireHighSecuritySession($viewer, $request, $cancel_uri); if ($request->isFormPost()) { $default_name = $key->getObject()->getSSHKeyDefaultName(); $keys = PhabricatorSSHKeyGenerator::generateKeypair(); list($public_key, $private_key) = $keys; $file = PhabricatorFile::buildFromFileDataOrHash($private_key, array('name' => $default_name . '.key', 'ttl' => time() + 60 * 10, 'viewPolicy' => $viewer->getPHID())); $public_key = PhabricatorAuthSSHPublicKey::newFromRawKey($public_key); $type = $public_key->getType(); $body = $public_key->getBody(); $key->setName($default_name)->setKeyType($type)->setKeyBody($body)->setKeyComment(pht('Generated'))->save(); // NOTE: We're disabling workflow on submit so the download works. We're // disabling workflow on cancel so the page reloads, showing the new // key. return $this->newDialog()->setTitle(pht('Download Private Key'))->setDisableWorkflowOnCancel(true)->setDisableWorkflowOnSubmit(true)->setSubmitURI($file->getDownloadURI())->appendParagraph(pht('A keypair has been generated, and the public key has been ' . 'added as a recognized key. Use the button below to download ' . 'the private key.'))->appendParagraph(pht('After you download the private key, it will be destroyed. ' . 'You will not be able to retrieve it if you lose your copy.'))->addSubmitButton(pht('Download Private Key'))->addCancelButton($cancel_uri, pht('Done')); } try { PhabricatorSSHKeyGenerator::assertCanGenerateKeypair(); return $this->newDialog()->setTitle(pht('Generate New Keypair'))->addHiddenInput('objectPHID', $key->getObject()->getPHID())->appendParagraph(pht('This workflow will generate a new SSH keypair, add the public ' . 'key, and let you download the private key.'))->appendParagraph(pht('Phabricator will not retain a copy of the private key.'))->addSubmitButton(pht('Generate New Keypair'))->addCancelButton($cancel_uri); } catch (Exception $ex) { return $this->newDialog()->setTitle(pht('Unable to Generate Keys'))->appendParagraph($ex->getMessage())->addCancelButton($cancel_uri); } }
public function handleRequest(AphrontRequest $request) { $viewer = $request->getUser(); $file = PhabricatorFile::initializeNewFile(); $e_file = true; $errors = array(); if ($request->isFormPost()) { $view_policy = $request->getStr('viewPolicy'); if (!$request->getFileExists('file')) { $e_file = pht('Required'); $errors[] = pht('You must select a file to upload.'); } else { $file = PhabricatorFile::newFromPHPUpload(idx($_FILES, 'file'), array('name' => $request->getStr('name'), 'authorPHID' => $viewer->getPHID(), 'viewPolicy' => $view_policy, 'isExplicitUpload' => true)); } if (!$errors) { return id(new AphrontRedirectResponse())->setURI($file->getInfoURI()); } $file->setViewPolicy($view_policy); } $support_id = celerity_generate_unique_node_id(); $instructions = id(new AphrontFormMarkupControl())->setControlID($support_id)->setControlStyle('display: none')->setValue(hsprintf('<br /><br /><strong>%s</strong> %s<br /><br />', pht('Drag and Drop:'), pht('You can also upload files by dragging and dropping them from your ' . 'desktop onto this page or the Phabricator home page.'))); $policies = id(new PhabricatorPolicyQuery())->setViewer($viewer)->setObject($file)->execute(); $form = id(new AphrontFormView())->setUser($viewer)->setEncType('multipart/form-data')->appendChild(id(new AphrontFormFileControl())->setLabel(pht('File'))->setName('file')->setError($e_file))->appendChild(id(new AphrontFormTextControl())->setLabel(pht('Name'))->setName('name')->setValue($request->getStr('name')))->appendChild(id(new AphrontFormPolicyControl())->setUser($viewer)->setCapability(PhabricatorPolicyCapability::CAN_VIEW)->setPolicyObject($file)->setPolicies($policies)->setName('viewPolicy'))->appendChild(id(new AphrontFormSubmitControl())->setValue(pht('Upload'))->addCancelButton('/file/'))->appendChild($instructions); $crumbs = $this->buildApplicationCrumbs(); $crumbs->addTextCrumb(pht('Upload'), $request->getRequestURI()); $crumbs->setBorder(true); $title = pht('Upload File'); $global_upload = id(new PhabricatorGlobalUploadTargetView())->setUser($viewer)->setShowIfSupportedID($support_id); $form_box = id(new PHUIObjectBoxView())->setHeaderText(pht('File'))->setFormErrors($errors)->setBackground(PHUIObjectBoxView::BLUE_PROPERTY)->setForm($form); $header = id(new PHUIHeaderView())->setHeader($title)->setHeaderIcon('fa-upload'); $view = id(new PHUITwoColumnView())->setHeader($header)->setFooter(array($form_box, $global_upload)); return $this->newPage()->setTitle($title)->setCrumbs($crumbs)->appendChild($view); }
public function handleRequest(AphrontRequest $request) { // No CSRF for Mailgun. $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites(); if (!$this->verifyMessage()) { throw new Exception(pht('Mail signature is not valid. Check your Mailgun API key.')); } $user = $request->getUser(); $raw_headers = $request->getStr('headers'); $raw_headers = explode("\n", rtrim($raw_headers)); $raw_dict = array(); foreach (array_filter($raw_headers) as $header) { list($name, $value) = explode(':', $header, 2); $raw_dict[$name] = ltrim($value); } $headers = array('to' => $request->getStr('recipient'), 'from' => $request->getStr('from'), 'subject' => $request->getStr('subject')) + $raw_dict; $received = new PhabricatorMetaMTAReceivedMail(); $received->setHeaders($headers); $received->setBodies(array('text' => $request->getStr('stripped-text'), 'html' => $request->getStr('stripped-html'))); $file_phids = array(); foreach ($_FILES as $file_raw) { try { $file = PhabricatorFile::newFromPHPUpload($file_raw, array('viewPolicy' => PhabricatorPolicies::POLICY_NOONE)); $file_phids[] = $file->getPHID(); } catch (Exception $ex) { phlog($ex); } } $received->setAttachments($file_phids); $received->save(); $received->processReceivedMail(); $response = new AphrontWebpageResponse(); $response->setContent(pht("Got it! Thanks, Mailgun!\n")); return $response; }
protected function willFilterPage(array $merchants) { $query = id(new PhabricatorEdgeQuery())->withSourcePHIDs(mpull($merchants, 'getPHID'))->withEdgeTypes(array(PhortuneMerchantHasMemberEdgeType::EDGECONST)); $query->execute(); foreach ($merchants as $merchant) { $member_phids = $query->getDestinationPHIDs(array($merchant->getPHID())); $member_phids = array_reverse($member_phids); $merchant->attachMemberPHIDs($member_phids); } if ($this->needProfileImage) { $default = null; $file_phids = mpull($merchants, 'getProfileImagePHID'); $file_phids = array_filter($file_phids); if ($file_phids) { $files = id(new PhabricatorFileQuery())->setParentQuery($this)->setViewer($this->getViewer())->withPHIDs($file_phids)->execute(); $files = mpull($files, null, 'getPHID'); } else { $files = array(); } foreach ($merchants as $merchant) { $file = idx($files, $merchant->getProfileImagePHID()); if (!$file) { if (!$default) { $default = PhabricatorFile::loadBuiltin($this->getViewer(), 'merchant.png'); } $file = $default; } $merchant->attachProfileImageFile($file); } } return $merchants; }
public function processRequest() { $request = $this->getRequest(); if ($request->isFormPost()) { $parser = new ArcanistDiffParser(); $diff = null; try { $diff = PhabricatorFile::readUploadedFileData($_FILES['diff-file']); } catch (Exception $ex) { $diff = $request->getStr('diff'); } $changes = $parser->parseDiff($diff); $diff = DifferentialDiff::newFromRawChanges($changes); $diff->setLintStatus(DifferentialLintStatus::LINT_SKIP); $diff->setUnitStatus(DifferentialLintStatus::LINT_SKIP); $diff->setAuthorPHID($request->getUser()->getPHID()); $diff->setCreationMethod('web'); $diff->save(); return id(new AphrontRedirectResponse())->setURI('/differential/diff/' . $diff->getID() . '/'); } $form = new AphrontFormView(); $arcanist_href = PhabricatorEnv::getDoclink('article/Arcanist_User_Guide.html'); $arcanist_link = phutil_render_tag('a', array('href' => $arcanist_href, 'target' => '_blank'), 'Arcanist'); $form->setAction('/differential/diff/create/')->setEncType('multipart/form-data')->setUser($request->getUser())->appendChild('<p class="aphront-form-instructions">The best way to create a ' . "Differential diff is by using {$arcanist_link}, but you " . 'can also just paste a diff (e.g., from <tt>svn diff</tt> or ' . '<tt>git diff</tt>) into this box or upload it as a file if you ' . 'really want.</p>')->appendChild(id(new AphrontFormTextAreaControl())->setLabel('Raw Diff')->setName('diff')->setHeight(AphrontFormTextAreaControl::HEIGHT_VERY_TALL))->appendChild(id(new AphrontFormFileControl())->setLabel('Raw Diff from file')->setName('diff-file'))->appendChild(id(new AphrontFormSubmitControl())->setValue("Create Diff »")); $panel = new AphrontPanelView(); $panel->setHeader('Create New Diff'); $panel->appendChild($form); $panel->setWidth(AphrontPanelView::WIDTH_FORM); return $this->buildStandardPageResponse($panel, array('title' => 'Create Diff', 'tab' => 'create')); }
protected function didFilterPage(array $blogs) { if ($this->needProfileImage) { $default = null; $file_phids = mpull($blogs, 'getProfileImagePHID'); $file_phids = array_filter($file_phids); if ($file_phids) { $files = id(new PhabricatorFileQuery())->setParentQuery($this)->setViewer($this->getViewer())->withPHIDs($file_phids)->execute(); $files = mpull($files, null, 'getPHID'); } else { $files = array(); } foreach ($blogs as $blog) { $file = idx($files, $blog->getProfileImagePHID()); if (!$file) { if (!$default) { $default = PhabricatorFile::loadBuiltin($this->getViewer(), 'blog.png'); } $file = $default; } $blog->attachProfileImageFile($file); } } return $blogs; }
protected function execute(ConduitAPIRequest $request) { $viewer = $request->getUser(); $hash = $request->getValue('contentHash'); $name = $request->getValue('name'); $view_policy = $request->getValue('viewPolicy'); $length = $request->getValue('contentLength'); $properties = array('name' => $name, 'authorPHID' => $viewer->getPHID(), 'viewPolicy' => $view_policy, 'isExplicitUpload' => true); $ttl = $request->getValue('deleteAfterEpoch'); if ($ttl) { $properties['ttl'] = $ttl; } $file = null; if ($hash) { $file = PhabricatorFile::newFileFromContentHash($hash, $properties); } if ($hash && !$file) { $chunked_hash = PhabricatorChunkedFileStorageEngine::getChunkedHash($viewer, $hash); $file = id(new PhabricatorFileQuery())->setViewer($viewer)->withContentHashes(array($chunked_hash))->executeOne(); } if (strlen($name) && !$hash && !$file) { if ($length > PhabricatorFileStorageEngine::getChunkThreshold()) { // If we don't have a hash, but this file is large enough to store in // chunks and thus may be resumable, try to find a partially uploaded // file by the same author with the same name and same length. This // allows us to resume uploads in Javascript where we can't efficiently // compute file hashes. $file = id(new PhabricatorFileQuery())->setViewer($viewer)->withAuthorPHIDs(array($viewer->getPHID()))->withNames(array($name))->withLengthBetween($length, $length)->withIsPartial(true)->setLimit(1)->executeOne(); } } if ($file) { return array('upload' => (bool) $file->getIsPartial(), 'filePHID' => $file->getPHID()); } // If there are any non-chunk engines which this file can fit into, // just tell the client to upload the file. $engines = PhabricatorFileStorageEngine::loadStorageEngines($length); if ($engines) { return array('upload' => true, 'filePHID' => null); } // Otherwise, this is a large file and we want to perform a chunked // upload if we have a chunk engine available. $chunk_engines = PhabricatorFileStorageEngine::loadWritableChunkEngines(); if ($chunk_engines) { $chunk_properties = $properties; if ($hash) { $chunk_properties += array('chunkedHash' => $chunked_hash); } $chunk_engine = head($chunk_engines); $file = $chunk_engine->allocateChunks($length, $chunk_properties); return array('upload' => true, 'filePHID' => $file->getPHID()); } // None of the storage engines can accept this file. if (PhabricatorFileStorageEngine::loadWritableEngines()) { $error = pht('Unable to upload file: this file is too large for any ' . 'configured storage engine.'); } else { $error = pht('Unable to upload file: the server is not configured with any ' . 'writable storage engines.'); } return array('upload' => false, 'filePHID' => null, 'error' => $error); }
public function handleRequest(AphrontRequest $request) { $viewer = $request->getViewer(); $id = $request->getURIData('id'); $this->requireApplicationCapability(PhabricatorMacroManageCapability::CAPABILITY); $macro = id(new PhabricatorMacroQuery())->setViewer($viewer)->requireCapabilities(array(PhabricatorPolicyCapability::CAN_VIEW))->withIDs(array($id))->executeOne(); if (!$macro) { return new Aphront404Response(); } $errors = array(); $view_uri = $this->getApplicationURI('/view/' . $macro->getID() . '/'); $e_file = null; $file = null; if ($request->isFormPost()) { $xactions = array(); if ($request->getBool('behaviorForm')) { $xactions[] = id(new PhabricatorMacroTransaction())->setTransactionType(PhabricatorMacroTransaction::TYPE_AUDIO_BEHAVIOR)->setNewValue($request->getStr('audioBehavior')); } else { $file = null; if ($request->getFileExists('file')) { $file = PhabricatorFile::newFromPHPUpload($_FILES['file'], array('name' => $request->getStr('name'), 'authorPHID' => $viewer->getPHID(), 'isExplicitUpload' => true)); } if ($file) { if (!$file->isAudio()) { $errors[] = pht('You must upload audio.'); $e_file = pht('Invalid'); } else { $xactions[] = id(new PhabricatorMacroTransaction())->setTransactionType(PhabricatorMacroTransaction::TYPE_AUDIO)->setNewValue($file->getPHID()); } } else { $errors[] = pht('You must upload an audio file.'); $e_file = pht('Required'); } } if (!$errors) { id(new PhabricatorMacroEditor())->setActor($viewer)->setContinueOnNoEffect(true)->setContentSourceFromRequest($request)->applyTransactions($macro, $xactions); return id(new AphrontRedirectResponse())->setURI($view_uri); } } $form = id(new AphrontFormView())->addHiddenInput('behaviorForm', 1)->setUser($viewer); $options = id(new AphrontFormRadioButtonControl())->setLabel(pht('Audio Behavior'))->setName('audioBehavior')->setValue(nonempty($macro->getAudioBehavior(), PhabricatorFileImageMacro::AUDIO_BEHAVIOR_NONE)); $options->addButton(PhabricatorFileImageMacro::AUDIO_BEHAVIOR_NONE, pht('Do Not Play'), pht('Do not play audio.')); $options->addButton(PhabricatorFileImageMacro::AUDIO_BEHAVIOR_ONCE, pht('Play Once'), pht('Play audio once, when the viewer looks at the macro.')); $options->addButton(PhabricatorFileImageMacro::AUDIO_BEHAVIOR_LOOP, pht('Play Continuously'), pht('Play audio continuously, treating the macro as an audio source. ' . 'Best for ambient sounds.')); $form->appendChild($options); $form->appendChild(id(new AphrontFormSubmitControl())->setValue(pht('Save Audio Behavior'))->addCancelButton($view_uri)); $crumbs = $this->buildApplicationCrumbs(); $title = pht('Edit Audio: %s', $macro->getName()); $crumb = pht('Edit Audio'); $crumbs->addTextCrumb(pht('Macro "%s"', $macro->getName()), $view_uri); $crumbs->addTextCrumb($crumb, $request->getRequestURI()); $crumbs->setBorder(true); $upload_form = id(new AphrontFormView())->setEncType('multipart/form-data')->setUser($viewer)->appendChild(id(new AphrontFormFileControl())->setLabel(pht('Audio File'))->setName('file'))->appendChild(id(new AphrontFormSubmitControl())->setValue(pht('Upload File'))); $upload = id(new PHUIObjectBoxView())->setHeaderText(pht('Upload New Audio'))->setBackground(PHUIObjectBoxView::BLUE_PROPERTY)->setForm($upload_form); $form_box = id(new PHUIObjectBoxView())->setHeaderText(pht('Behavior'))->setFormErrors($errors)->setBackground(PHUIObjectBoxView::BLUE_PROPERTY)->setForm($form); $header = id(new PHUIHeaderView())->setHeader($title)->setHeaderIcon('fa-pencil'); $view = id(new PHUITwoColumnView())->setHeader($header)->setFooter(array($form_box, $upload)); return $this->newPage()->setTitle($title)->setCrumbs($crumbs)->appendChild($view); }
public function getTransformedDimensions(PhabricatorFile $file) { $dst_x = $this->dstX; $dst_y = $this->dstY; // If this is transform has fixed dimensions, we can trivially predict // the dimensions of the transformed file. if ($dst_y !== null) { return array($dst_x, $dst_y); } $src_x = $file->getImageWidth(); $src_y = $file->getImageHeight(); if (!$src_x || !$src_y) { return null; } $dimensions = $this->computeDimensions($src_x, $src_y, $dst_x, $dst_y); return array($dimensions['dst_x'], $dimensions['dst_y']); }
private function generatePreview(PhabricatorFile $file, $size) { $data = $file->loadFileData(); $src = imagecreatefromstring($data); $x = imagesx($src); $y = imagesy($src); $scale = min($size / $x, $size / $y, 1); $dx = max($size / 4, $scale * $x); $dy = max($size / 4, $scale * $y); $dst = imagecreatetruecolor($dx, $dy); imagesavealpha($dst, true); imagefill($dst, 0, 0, imagecolorallocatealpha($dst, 255, 255, 255, 127)); $sdx = $scale * $x; $sdy = $scale * $y; imagecopyresampled($dst, $src, ($dx - $sdx) / 2, ($dy - $sdy) / 2, 0, 0, $sdx, $sdy, $x, $y); return $this->saveImageDataInAnyFormat($dst, $file->getMimeType()); }
public function saveFile($path, $name) { $data = $this->readFile($path); $file = PhabricatorFile::newFromFileData($data, array('name' => $name)); $file->setName($name); $file->save(); return $file; }
private function buildPropertyViews(PHUIObjectBoxView $box, PhabricatorFile $file, PhabricatorActionListView $actions) { $request = $this->getRequest(); $user = $request->getUser(); $properties = id(new PHUIPropertyListView()); $properties->setActionList($actions); $box->addPropertyList($properties, pht('Details')); if ($file->getAuthorPHID()) { $properties->addProperty(pht('Author'), $this->getHandle($file->getAuthorPHID())->renderLink()); } $properties->addProperty(pht('Created'), phabricator_datetime($file->getDateCreated(), $user)); $finfo = id(new PHUIPropertyListView()); $box->addPropertyList($finfo, pht('File Info')); $finfo->addProperty(pht('Size'), phutil_format_bytes($file->getByteSize())); $finfo->addProperty(pht('Mime Type'), $file->getMimeType()); $width = $file->getImageWidth(); if ($width) { $finfo->addProperty(pht('Width'), pht('%s px', new PhutilNumber($width))); } $height = $file->getImageHeight(); if ($height) { $finfo->addProperty(pht('Height'), pht('%s px', new PhutilNumber($height))); } $is_image = $file->isViewableImage(); if ($is_image) { $image_string = pht('Yes'); $cache_string = $file->getCanCDN() ? pht('Yes') : pht('No'); } else { $image_string = pht('No'); $cache_string = pht('Not Applicable'); } $finfo->addProperty(pht('Viewable Image'), $image_string); $finfo->addProperty(pht('Cacheable'), $cache_string); $storage_properties = new PHUIPropertyListView(); $box->addPropertyList($storage_properties, pht('Storage')); $storage_properties->addProperty(pht('Engine'), $file->getStorageEngine()); $storage_properties->addProperty(pht('Format'), $file->getStorageFormat()); $storage_properties->addProperty(pht('Handle'), $file->getStorageHandle()); $phids = $file->getObjectPHIDs(); if ($phids) { $attached = new PHUIPropertyListView(); $box->addPropertyList($attached, pht('Attached')); $attached->addProperty(pht('Attached To'), $this->renderHandlesForPHIDs($phids)); } if ($file->isViewableImage()) { $image = phutil_tag('img', array('src' => $file->getViewURI(), 'class' => 'phui-property-list-image')); $linked_image = phutil_tag('a', array('href' => $file->getViewURI()), $image); $media = id(new PHUIPropertyListView())->addImageContent($linked_image); $box->addPropertyList($media); } else { if ($file->isAudio()) { $audio = phutil_tag('audio', array('controls' => 'controls', 'class' => 'phui-property-list-audio'), phutil_tag('source', array('src' => $file->getViewURI(), 'type' => $file->getMimeType()))); $media = id(new PHUIPropertyListView())->addImageContent($audio); $box->addPropertyList($media); } } }
private function generateTestFile(PhabricatorUser $actor) { $engine = new PhabricatorTestStorageEngine(); $data = Filesystem::readRandomCharacters(64); $params = array('name' => 'test.' . $actor->getPHID(), 'viewPolicy' => $actor->getPHID(), 'authorPHID' => $actor->getPHID(), 'storageEngines' => array($engine)); $file = PhabricatorFile::newFromFileData($data, $params); $file->save(); return $file; }
protected function execute(ConduitAPIRequest $request) { $data = $request->getValue('data_base64'); $name = $request->getValue('name'); $data = base64_decode($data, $strict = true); $user = $request->getUser(); $file = PhabricatorFile::newFromFileData($data, array('name' => $name, 'authorPHID' => $user->getPHID())); return $file->getPHID(); }
public function handleRequest(AphrontRequest $request) { $viewer = $request->getViewer(); $aid = $request->getURIData('aid'); $bid = $request->getURIData('bid'); // If "aid" is "x", then it means the user wants to generate // a patch of an empty file to the version specified by "bid". $ids = array($aid, $bid); if ($aid === 'x') { $ids = array($bid); } $versions = id(new PhragmentFragmentVersionQuery())->setViewer($viewer)->withIDs($ids)->execute(); $version_a = null; if ($aid !== 'x') { $version_a = idx($versions, $aid, null); if ($version_a === null) { return new Aphront404Response(); } } $version_b = idx($versions, $bid, null); if ($version_b === null) { return new Aphront404Response(); } $file_phids = array(); if ($version_a !== null) { $file_phids[] = $version_a->getFilePHID(); } $file_phids[] = $version_b->getFilePHID(); $files = id(new PhabricatorFileQuery())->setViewer($viewer)->withPHIDs($file_phids)->execute(); $files = mpull($files, null, 'getPHID'); $file_a = null; if ($version_a != null) { $file_a = idx($files, $version_a->getFilePHID(), null); } $file_b = idx($files, $version_b->getFilePHID(), null); $patch = PhragmentPatchUtil::calculatePatch($file_a, $file_b); if ($patch === null) { // There are no differences between the two files, so we output // an empty patch. $patch = ''; } $a_sequence = 'x'; if ($version_a !== null) { $a_sequence = $version_a->getSequence(); } $name = $version_b->getFragment()->getName() . '.' . $a_sequence . '.' . $version_b->getSequence() . '.patch'; $return = $version_b->getURI(); if ($request->getExists('return')) { $return = $request->getStr('return'); } $result = PhabricatorFile::buildFromFileDataOrHash($patch, array('name' => $name, 'mime-type' => 'text/plain', 'ttl' => time() + 60 * 60 * 24)); $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites(); $result->attachToObject($version_b->getFragmentPHID()); unset($unguarded); return id(new AphrontRedirectResponse())->setURI($result->getDownloadURI($return)); }
public function generate() { $author_phid = $this->loadPhabrictorUserPHID(); $dimension = 1 << rand(5, 12); $image = id(new PhabricatorLipsumMondrianArtist())->generate($dimension, $dimension); $file = PhabricatorFile::newFromFileData($image, array('name' => 'rand-' . rand(1000, 9999))); $file->setAuthorPHID($author_phid); $file->setMimeType('image/jpeg'); return $file->save(); }
private function applyMemeToFile(PhabricatorFile $file, $upper_text, $lower_text) { $data = $file->loadFileData(); $img_type = $file->getMimeType(); $imagemagick = PhabricatorEnv::getEnvConfig('files.enable-imagemagick'); if ($img_type != 'image/gif' || $imagemagick == false) { return $this->applyMemeTo($data, $upper_text, $lower_text, $img_type); } $data = $file->loadFileData(); $input = new TempFile(); Filesystem::writeFile($input, $data); list($out) = execx('convert %s info:', $input); $split = phutil_split_lines($out); if (count($split) > 1) { return $this->applyMemeWithImagemagick($input, $upper_text, $lower_text, count($split), $img_type); } else { return $this->applyMemeTo($data, $upper_text, $lower_text, $img_type); } }