Example #1
0
 /**
  * Handle GET requests.
  *
  * @param Alpha\Util\Http\Request $request
  *
  * @since 1.0
  *
  * @throws Alpha\Exception\ResourceNotFoundException
  */
 public function doGET($request)
 {
     self::$logger->debug('>>doGET($request=[' . var_export($request, true) . '])');
     $config = ConfigProvider::getInstance();
     $params = $request->getParams();
     try {
         if (isset($params['articleOID']) && isset($params['filename'])) {
             if (!Validator::isInteger($params['articleOID'])) {
                 throw new IllegalArguementException('The articleOID [' . $params['articleOID'] . '] provided is invalid');
             }
             $article = new Article();
             $article->setOID($params['articleOID']);
             $filePath = $article->getAttachmentsLocation() . '/' . $params['filename'];
             if (file_exists($filePath)) {
                 self::$logger->info('Downloading the file [' . $params['filename'] . '] from the folder [' . $article->getAttachmentsLocation() . ']');
                 $pathParts = pathinfo($filePath);
                 $mimeType = FileUtils::getMIMETypeByExtension($pathParts['extension']);
                 $response = new Response(200, file_get_contents($filePath));
                 $response->setHeader('Content-Type', $mimeType);
                 $response->setHeader('Content-Disposition', 'attachment; filename="' . $pathParts['basename'] . '"');
                 $response->setHeader('Content-Length', filesize($filePath));
                 self::$logger->debug('<<doGET');
                 return $response;
             } else {
                 self::$logger->error('Could not access article attachment file [' . $filePath . '] as it does not exist!');
                 throw new IllegalArguementException('File not found');
             }
         } else {
             self::$logger->error('Could not access article attachment as articleOID and/or filename were not provided!');
             throw new IllegalArguementException('File not found');
         }
     } catch (IllegalArguementException $e) {
         self::$logger->error($e->getMessage());
         throw new ResourceNotFoundException($e->getMessage());
     }
     self::$logger->debug('<<doGET');
 }