Page MenuHomePhorge

No OneTemporary

diff --git a/src/conduit/DiagramUploadConduitAPIMethod.php b/src/conduit/DiagramUploadConduitAPIMethod.php
index e8cfa86..d2d6f46 100644
--- a/src/conduit/DiagramUploadConduitAPIMethod.php
+++ b/src/conduit/DiagramUploadConduitAPIMethod.php
@@ -1,127 +1,176 @@
<?php
final class DiagramUploadConduitAPIMethod extends ConduitAPIMethod {
/**
* Name of the Conduit API method.
* This method should return a string that represents the name of the method.
* The method name is used to identify the method when calling it via the
* Conduit API.
*/
public function getAPIMethodName() {
return 'diagram.upload';
}
/**
* Defines the input parameters for this API.
* This method should return an associative array where the keys are
* the names of the parameters and the values are strings that describe
* the types of the parameters.
* The parameter types are used for documentation purposes and are displayed
* on the documentation page for the method to help users understand what
* kind of data they can pass to the method when calling it.
*/
protected function defineParamTypes() {
return array(
'data_base64' => 'required nonempty base64-bytes',
'id' => 'optional id of diagram to be updated',
'viewPolicy' => 'optional valid policy string or <phid>',
);
}
/**
* This method should return a string that describes the type of data that
* the method returns.
* The return type is used for documentation purposes and is displayed on the
* documentation page for the method to help users understand what kind of
* data they can expect to receive when calling the method.
*/
protected function defineReturnType() {
return 'nonempty guid';
}
/**
* The execute method is the core of your Conduit API method and is where
* you implement the logic that performs the desired operation.
*
* The execute method should return the result of the operation in a
* format that can be serialized as JSON.
* The result will be sent back to the client as the response to the API
* request.
*/
protected function execute(ConduitAPIRequest $request) {
$viewer = $request->getUser();
$base64_data = $request->getValue('data_base64');
$diagram_id = $request->getValue('id');
if ($diagram_id) {
// check if we are trying to save the same data as the current data
$diagram = id(new DiagramVersion())->loadLatestByDiagramID($diagram_id);
if ($diagram !== null) {
$data = $diagram->getData();
$old_base64_data = base64_encode($data);
if (DiagramController::equalPngMetaData($base64_data, $old_base64_data)) {
return array(
'result' => $diagram->getPHID(),
'id' => $diagram->getDiagramID(),
'error_code' => null,
'error_info' => null
);
}
}
}
// verify if the uploaded data really contains a drawio diagram
if (DiagramController::isDrawioPngBase64($base64_data) == false) {
return array(
'result' => null,
'id' => null,
'error_code' => -1,
'error_info' => 'invalid base64 data'
);
}
// Set the options for the new file
$options = array(
'name' => 'diagram.png',
'viewPolicy' => PhabricatorPolicies::POLICY_USER,
'mime-type' => 'image/png',
'actor' => $this->getViewer(),
'diagramID' => $diagram_id
);
try {
// Create the new file object
$diagram = DiagramVersion::newFromFileData($base64_data, $options);
$diagram->publishNewVersion($request, $diagram->getDiagramID());
return array(
'result' => $diagram->getPHID(),
'id' => $diagram->getDiagramID(),
'error_code' => null,
'error_info' => null
);
} catch (Exception $e) {
// error occurred during saving
return array(
'result' => null,
'id' => null,
'error_code' => $e->getCode(),
'error_info' => $e->getMessage()
);
}
}
/**
* Descriptive text what this Conduit API method does
* This method should return a string that describes what the method does.
* The method description is used for documentation purposes and is
* displayed on the documentation page for the method to help users
* understand what the method does and how to use it.
*/
public function getMethodDescription() {
- return pht('Upload a diagram to the server.');
+ return pht("Upload a diagram to the server.");
+ }
+
+ /**
+ * Generates custom documentation pages for this Conduit API method.
+ * This method should return an array of PhabricatorDocumentationBoxPage
+ * objects representing the custom documentation pages you want to add for
+ * the method.
+ * Each PhabricatorDocumentationBoxPage object represents a single
+ * documentation page and includes a title and content.
+ */
+ public function newDocumentationPages(PhabricatorUser $viewer) {
+ $pages = array();
+
+ // create different chapters
+ $pages[] = $this->getDocumentationUsage($viewer);
+
+ return $pages;
+ }
+
+ /**
+ * Creates the documentation chapter about Usage
+ */
+ public function getDocumentationUsage(PhabricatorUser $viewer) {
+ // set title and content of 'Usage' documentation box
+ $title = pht('Usage');
+ $content = pht(<<<EOREMARKUP
+IMPORTANT: when sending Base64 encoded data in `data_base64` using the
+`application/x-www-form-urlencoded` content type, the `+` character is used to
+represent a space character. To ensure that the `+` character is correctly
+interpreted by the server as a `+` character and not a space character, it
+should be URL-encoded as **%%2B**
+EOREMARKUP
+ );
+
+ // format content
+ $content = $this->newRemarkupDocumentationView($content);
+
+ // create documentation box
+ $page = $this->newDocumentationBoxPage(
+ $viewer,
+ $title,
+ $content
+ );
+
+ // set icon and anchor of documentation box for navigation menu on the left
+ $page->setAnchor('usage');
+ $page->setIconIcon('fa-warning');
+
+ return $page;
}
}

File Metadata

Mime Type
text/x-diff
Expires
Sun, Jan 19, 13:26 (3 w, 3 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1125056
Default Alt Text
(6 KB)

Event Timeline