/**
  * Function serves the attachment by provided guid and exits.
  * @todo: Permission handling
  * @todo: Direct filesystem serving
  * @todo: Configuration options
  */
 public function get_serve(array $args)
 {
     $att = new midgard_attachment($args['guid']);
     if (midgardmvc_core::get_instance()->configuration->enable_attachment_cache) {
         midgardmvc_core::get_instance()->dispatcher->header('Location: ' . midgardmvc_core_helpers_attachment::get_url($att));
         midgardmvc_core::get_instance()->dispatcher->end_request();
     }
     $blob = new midgard_blob($att);
     midgardmvc_core::get_instance()->dispatcher->header('Content-type: ' . $att->mimetype);
     /**
      * If X-Sendfile support is enabled just sending correct headers
      */
     if (midgardmvc_core::get_instance()->configuration->enable_xsendfile) {
         midgardmvc_core::get_instance()->dispatcher->header('X-Sendfile: ' . $blob->get_path());
     } else {
         echo $blob->read_content();
     }
     midgardmvc_core::get_instance()->dispatcher->end_request();
 }
Exemple #2
0
 /**
  * This function reads the file and returns its contents
  *
  * @return string
  */
 public function read()
 {
     $attachment = new midgard_attachment($this->guid);
     $blob = new midgard_blob($attachment);
     $contents = $blob->read_content();
     return $contents;
 }
 /**
  * Creates or updates a package in the database
  *
  * @return object package object
  */
 public function createPackage($project_name = null, $repo_id = null, $repo_name = null, $arch_name = null, $package_name = null, $file_name = null, $repo_arch_name = null)
 {
     if ($project_name && $repo_name && $arch_name && $package_name && $file_name) {
         // get fill package info via OBS API
         try {
             $extinfo = $this->api->getPackageWithFullInformation($project_name, $repo_name, $arch_name, $package_name, $file_name);
         } catch (RuntimeException $e) {
             $this->log('         [EXCEPTION] ' . $e->getMessage());
         }
     } else {
         throw new RuntimeException('Not enough parameters to gather full package information');
         return;
     }
     // get a com_meego_package instance
     $package = $this->getPackageByFileName($file_name, $repo_id);
     if ($package && $repo_id && $extinfo) {
         if (!$package->guid) {
             $package->repository = $repo_id;
             $package->filename = $extinfo->filename;
         }
         // deb or rpm
         $package->type = substr($package->filename, strrpos($package->filename, '.') + 1);
         $package->size = $extinfo->size;
         $package->name = $extinfo->name;
         $package->title = $extinfo->title;
         $package->parent = $package_name;
         $package->version = $extinfo->version;
         $summary_max_length = 100;
         if (array_key_exists('summary_max_length', $this->config)) {
             $summary_max_length = $this->config['summary_max_length'];
         }
         if (strlen($extinfo->summary)) {
             $package->summary = $this->generateAbstract($extinfo->summary, $summary_max_length);
         } else {
             $package->summary = $this->generateAbstract($extinfo->description, $summary_max_length);
         }
         $package->description = $extinfo->description;
         // if the package is a source package then the downloadurl is slightly different
         // also change the title a bit
         if ($repo_arch_name == 'src') {
             $package->title = $package->title . '-src';
         }
         if ($repo_arch_name == 'armv7el') {
             // fix the inconsistency between the published repo and the API
             $repo_arch_name = 'armv7l';
             if ($package->type == 'deb') {
                 $repo_arch_name = $extinfo->arch;
             }
         }
         // direct download url
         $_uri = str_replace(':', ':/', $project_name) . '/' . str_replace(':', ':/', $repo_name) . '/' . $repo_arch_name . '/' . $file_name;
         $package->downloadurl = $this->download_repo_protocol . '://' . $this->download_repo_host . '/' . $_uri;
         // @todo
         $package->bugtracker = '* TODO *';
         // for some info we need a special xray
         try {
             switch ($package->type) {
                 case 'rpm':
                     $xray = new RpmXray($this->download_repo_protocol, $this->download_repo_host, $_uri);
                     break;
                 case 'deb':
                     $xray = new DebXray($this->download_repo_protocol, $this->download_repo_host, $_uri, $this->config['wget'], $this->config['wget_options'], $this->debug);
                     break;
                 default:
                     throw new RuntimeException("Unknown file extension: " . $package->type . "(should be rpm or deb).");
             }
         } catch (RuntimeException $e) {
             $this->log('         [EXCEPTION] ' . $e->getMessage());
             // if there was a problem during xray (with code 999)
             // then it almost certainly means that the package no longer exists in the repository
             // so if the package exists in our database then remove it
             if ($package->guid && $e->getCode() == 999) {
                 // if package deletion is OK then return immediately
                 $result = $this->deletePackage($package, $project_name);
             }
         }
         if (is_object($xray)) {
             $package->license = $this->getLicense($xray->license, '');
             $package->homepageurl = $xray->url;
             $package->category = $this->getCategory($xray->group);
             if (isset($xray->title) && strlen($xray->title)) {
                 $package->title = $xray->title;
             }
         }
         // call the parent
         if ($package->guid) {
             $this->log('           update: ' . $package->filename . ' (title: ' . $package->title . ', guid: ' . $package->guid . ')');
             $package->metadata->hidden = false;
             $package->update();
         } else {
             if ($package->create()) {
                 $this->log('           create: ' . $package->filename . ' (title: ' . $package->title . ', guid: ' . $package->guid . ')');
             } else {
                 $this->log('           failed to create: ' . $package->filename . ' (title: ' . $package->title . ')');
             }
         }
         try {
             // if attachment creation failed then use the original OBS link
             $package->installfileurl = $this->api->getInstallFileURL($project_name, $repo_name, $arch_name, $package_name, $file_name);
             // get the file and store it locally
             // $fp might be a stream, or string if wget is in use
             $fp = $this->api->http->get_as_stream($this->api->getRelativeInstallPath($project_name, $repo_name, $arch_name, $package_name, $file_name));
             if ($fp) {
                 $attachment = $package->create_attachment($package_name . "_install.ymp", $package_name . "_install.ymp", "text/x-suse-ymp");
                 if ($attachment) {
                     $blob = new midgard_blob($attachment);
                     $handler = $blob->get_handler('wb');
                     if ($handler) {
                         if (!$this->config['wget']) {
                             $ymp = stream_get_contents($fp);
                         } else {
                             $ymp = $fp;
                         }
                         $origymp = $ymp;
                         // replace name with the package name
                         $ymp = self::replace_name_with_packagename($ymp, $package->name);
                         if (!strlen($ymp)) {
                             $this->log('           attempt to update package name in: ' . $attachment->name . ' would result in 0 byte long file; rollback, location: blobs/' . $attachment->location);
                             $ymp = $origymp;
                         }
                         // write the attachment to the file system
                         fwrite($handler, $ymp);
                         if (!$this->config['wget']) {
                             fclose($fp);
                         }
                         // close the attachment's handler
                         fclose($handler);
                         $attachment->update();
                         $this->log('           attachment created: ' . $attachment->name . ' (location: blobs/' . $attachment->location . ')');
                     } else {
                         $this->log('Could not create attachment');
                     }
                 } else {
                     // could not create attachment, maybe we have it already
                     $attachments = $package->list_attachments();
                     foreach ($attachments as $attachment) {
                         if ($attachment->name == $package_name . "_install.ymp") {
                             $blob = new midgard_blob($attachment);
                             $handler = $blob->get_handler('rb+');
                             if ($handler) {
                                 $content = $blob->read_content();
                                 $ymp = $content;
                                 if (strlen($content)) {
                                     $ymp = self::replace_name_with_packagename($content, $package->name);
                                 }
                                 if (!strlen($ymp)) {
                                     $this->log('           attempt to update package name in: ' . $attachment->name . ' would result in 0 byte long file; rollback, location: blobs/' . $attachment->location);
                                     $ymp = $content;
                                 }
                                 fwrite($handler, $ymp);
                                 fclose($handler);
                             } else {
                                 $this->log('           failed to update attachment: ' . $attachment->name . ', location: blobs/' . $attachment->location);
                             }
                             break;
                         }
                     }
                 }
                 // set the install url field to the local attachment
                 if (is_object($attachment)) {
                     // write a local relative URL for the install file
                     $package->installfileurl = '/mgd:attachment/' . $attachment->guid . '/' . $attachment->name;
                 }
                 // update because of the installfileurl stuff
                 $package->update();
             }
         } catch (RuntimeException $e) {
             $this->log('         [EXCEPTION] ' . $e->getMessage());
         }
         // get the roles and create the necessary role objects
         $roles = $this->api->getPackageMeta($project_name, $package_name);
         foreach ($roles as $role => $userids) {
             foreach ($userids as $userid) {
                 $this->log('           create role: ' . $userid . ' = ' . $role . ' (' . $package->guid . ')');
                 $this->createRole($package->guid, $userid, $role);
             }
         }
         // add relations by calling the parent class
         $this->addRelations($extinfo, $package);
     }
     return $package;
 }