Ejemplo n.º 1
0
 /**
  * Gets a single temporary file for rendering and removes it from filesystem.
  *
  * @param ServiceBase $api The service base
  * @param array $args Arguments array built by the service base
  */
 public function getTempImage($api, $args)
 {
     // Get the field
     if (empty($args['field'])) {
         // @TODO Localize this exception message
         throw new SugarApiExceptionMissingParameter('Field name is missing');
     }
     $field = $args['field'];
     // Get the bean
     $bean = BeanFactory::newBean($args['module']);
     // Handle ACL
     $this->verifyFieldAccess($bean, $field);
     $filepath = UploadStream::path("upload://tmp/") . $args['temp_id'];
     if (is_file($filepath)) {
         $filedata = getimagesize($filepath);
         $info = array('content-type' => $filedata['mime'], 'path' => $filepath);
         require_once "include/download_file.php";
         $dl = new DownloadFileApi($api);
         $dl->outputFile(false, $info);
         if (!empty($args['keep'])) {
             return;
         }
         register_shutdown_function(function () use($filepath) {
             if (is_file($filepath)) {
                 unlink($filepath);
             }
         });
     } else {
         throw new SugarApiExceptionInvalidParameter('File not found');
     }
 }
Ejemplo n.º 2
0
 /**
  * Gets the file information array for an uploaded file that is associated
  * with a bean's $field
  *
  * The $args array should have already been called prior to this method in
  * order to get full path URIs for the file
  *
  * @param SugarBean $bean The bean to get the info from
  * @param string $field The field name to get the file data from
  * @param ServiceBase $api The calling API service object
  * @return array|bool
  */
 protected function getFileInfo($bean, $field, $api)
 {
     $info = array();
     if (isset($bean->field_defs[$field])) {
         $def = $bean->field_defs[$field];
         if (isset($def['type']) && !empty($bean->{$field})) {
             if ($def['type'] == 'image') {
                 $filename = $bean->{$field};
                 $filepath = 'upload://' . $filename;
                 $filedata = getimagesize($filepath);
                 // Add in height and width for image types
                 $info = array('content-type' => $filedata['mime'], 'content-length' => filesize($filepath), 'name' => $filename, 'path' => $filepath, 'width' => empty($filedata[0]) ? 0 : $filedata[0], 'height' => empty($filedata[1]) ? 0 : $filedata[1], 'uri' => $api->getResourceURI(array($bean->module_dir, $bean->id, 'file', $field)));
             } elseif ($def['type'] == 'file') {
                 require_once 'include/download_file.php';
                 $download = new DownloadFileApi($api);
                 $info = $download->getFileInfo($bean, $field);
                 if (!empty($info) && empty($info['uri'])) {
                     $info['uri'] = $api->getResourceURI(array($bean->module_dir, $bean->id, 'file', $field));
                 }
             }
         }
     }
     return $info;
 }