Author: sevein
Date: Tue Oct 11 17:55:18 2011
New Revision: 10024

Log:
First steps in the Archivematica package extraction

Added:
   trunk/plugins/qtSwordPlugin/lib/qtPackageExtractor.class.php
   trunk/plugins/qtSwordPlugin/lib/qtSwordPlugin.class.php
   
trunk/plugins/qtSwordPlugin/modules/qtSwordPlugin/templates/error/ErrorMaxUploadSizeExceeded.xml.php
Modified:
   trunk/plugins/qtSwordPlugin/config/qtSwordPluginConfiguration.class.php
   
trunk/plugins/qtSwordPlugin/modules/qtSwordPlugin/actions/depositAction.class.php
   
trunk/plugins/qtSwordPlugin/modules/qtSwordPlugin/templates/depositSuccess.xml.php
   
trunk/plugins/qtSwordPlugin/modules/qtSwordPlugin/templates/servicedocumentSuccess.xml.php

Modified: 
trunk/plugins/qtSwordPlugin/config/qtSwordPluginConfiguration.class.php
==============================================================================
--- trunk/plugins/qtSwordPlugin/config/qtSwordPluginConfiguration.class.php     
Tue Oct 11 15:17:33 2011        (r10023)
+++ trunk/plugins/qtSwordPlugin/config/qtSwordPluginConfiguration.class.php     
Tue Oct 11 17:55:18 2011        (r10024)
@@ -25,7 +25,8 @@
 
     // See http://www.swordapp.org/docs/sword-type-1.0.html
     $packaging = array(
-      '1.0' => 'http://www.loc.gov/METS/'), // METS
+      // '1.0' => 'http://www.loc.gov/METS/'), // METS
+      '1.0' => 'http://purl.org/net/sword-types/METSArchivematicaDIP'), // METS
 
     $mediaRanges = array(
       'application/zip');

Added: trunk/plugins/qtSwordPlugin/lib/qtPackageExtractor.class.php
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ trunk/plugins/qtSwordPlugin/lib/qtPackageExtractor.class.php        Tue Oct 
11 17:55:18 2011        (r10024)
@@ -0,0 +1,68 @@
+<?php
+
+/*
+ * This file is part of Qubit Toolkit.
+ *
+ * Qubit Toolkit is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Qubit Toolkit is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Qubit Toolkit.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+class qtPackageExtractor
+{
+  protected
+    $directory = null,
+    $filename = null,
+    $document = null,
+    $format = null,
+    $type = null;
+
+  public function __construct($filename, array $options = array())
+  {
+    $this->filename = $filename;
+
+    if (isset($options['format']))
+    {
+      $this->format = $options['format'];
+    }
+
+    if (isset($options['type']))
+    {
+      $this->type = $options['type'];
+    }
+
+    $this->extract();
+
+    $this->document = new SimpleXMLElement($this->document);
+  }
+
+  protected function extract()
+  {
+    switch ($this->type)
+    {
+      case 'application/zip':
+        $this->directory = $this->filename.'_dir';
+        $command = vsprintf('unzip -d %s %s', array($this->directory, 
$this->filename));
+        exec($command, $output, $return);
+        if (0 == $return)
+        {
+          $this->document = @file_get_contents($this->directory.'/METS.xml');
+        }
+
+      break;
+    }
+  }
+
+  protected function getObjectContainer()
+  {
+  }
+}

Added: trunk/plugins/qtSwordPlugin/lib/qtSwordPlugin.class.php
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ trunk/plugins/qtSwordPlugin/lib/qtSwordPlugin.class.php     Tue Oct 11 
17:55:18 2011        (r10024)
@@ -0,0 +1,39 @@
+<?php
+
+/*
+ * This file is part of Qubit Toolkit.
+ *
+ * Qubit Toolkit is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Qubit Toolkit is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Qubit Toolkit.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+class qtSwordPlugin
+{
+  public static function saveRequestContent()
+  {
+    $filename = tempnam('/tmp', 'php_qubit_');
+
+    $file = fopen($filename, 'w');
+    $source = fopen("php://input", 'r');
+
+    while ($kb = fread($source, 1024))
+    {
+      fwrite($file, $kb, 1024);
+    }
+
+    fclose($file);
+    fclose($source);
+
+    return $filename;
+  }
+}

Modified: 
trunk/plugins/qtSwordPlugin/modules/qtSwordPlugin/actions/depositAction.class.php
==============================================================================
--- 
trunk/plugins/qtSwordPlugin/modules/qtSwordPlugin/actions/depositAction.class.php
   Tue Oct 11 15:17:33 2011        (r10023)
+++ 
trunk/plugins/qtSwordPlugin/modules/qtSwordPlugin/actions/depositAction.class.php
   Tue Oct 11 17:55:18 2011        (r10024)
@@ -20,12 +20,9 @@
 // TODO
 // Check user authorization
 // Check upload limit
-// Check 'Content-Type'
-// Check 'Content-Length' ?
 // Check attatchment size, zero?
-// Save the file temporary // Open the package (METS support?), get metadata 
and digital objects
-// Create persistent objects in the database
-// Generate routes and response with links, depositSuccess, etc...
+// Package
+// Response
 // Complete XML error documents (see templates/error/*)
 
 class qtSwordPluginDepositAction extends sfAction
@@ -47,10 +44,18 @@
       }
       */
 
-      $this->packaging = $request->getHttpHeader('X-Packaging');
+      $this->packageFormat = $request->getHttpHeader('X-Packaging');
+      $this->packageContentType = $request->getContentType();
+      // $this->packageContentLength = $request->getContentLength();
 
       // Check if the packaging format is supported
-      if (!in_array($this->packaging, qtSwordPluginConfiguration::$packaging))
+      if (!in_array($this->packageFormat, 
qtSwordPluginConfiguration::$packaging))
+      {
+        return $this->generateResponse(415, 'error/ErrorContent');
+      }
+
+      // Check if the content type is supported
+      if (!in_array($this->packageContentType, 
qtSwordPluginConfiguration::$mediaRanges))
       {
         return $this->generateResponse(415, 'error/ErrorContent');
       }
@@ -68,38 +73,33 @@
         return $this->generateResponse(400, 'error/ErrorBadRequest');
       }
 
-      // Package content
-      $package_content = @file_get_contents('php://input');
+      $filename = qtSwordPlugin::saveRequestContent();
 
       // Package name
       if (null !== $request->getHttpHeader('Content-Disposition'))
       {
-        $package_name = substr($request->getHttpHeader('Content-Disposition'), 
9);
+        $this->packageName = 
substr($request->getHttpHeader('Content-Disposition'), 9);
       }
       else
       {
-        // Server implementations MUST adopt the behaviour and requirements in 
[RFC2183].
-        $package_name = 'foobar';
+        // TODO name, [RFC2183]
       }
 
       // Calculated MD5 check does not match the value provided by the client
-      if (md5($package_content) != $request->getHttpHeader('Content-MD5'))
+      if (md5(file_get_contents($filename)) != 
$request->getHttpHeader('Content-MD5'))
       {
         return $this->generateResponse(412, 
'error/ErrorChecksumMismatchSuccess');
       }
 
-      // Create tmp dir, if it doesn't exist already
-      $tmpDir = sfConfig::get('sf_upload_dir').'/tmp';
-      if (!file_exists($tmpDir))
-      {
-        mkdir($tmpDir);
-        chmod($tmpDir, 0775);
-      }
-
-      return $this->generateResponse(201, 'deposit');
+      // Open the file, parse xml, get objects
+      $extractor = new qtPackageExtractor($filename, array('format' => 
$this->packageFormat, 'type' => $this->packageContentType));
 
       // Location
       // $this->response->setHttpHeader('Location', '...');
+
+      unlink($filename);
+
+      return $this->generateResponse(201, 'deposit');
     }
     else if ($request->isMethod('put') || $request->isMethod('delete'))
     {

Modified: 
trunk/plugins/qtSwordPlugin/modules/qtSwordPlugin/templates/depositSuccess.xml.php
==============================================================================
--- 
trunk/plugins/qtSwordPlugin/modules/qtSwordPlugin/templates/depositSuccess.xml.php
  Tue Oct 11 15:17:33 2011        (r10023)
+++ 
trunk/plugins/qtSwordPlugin/modules/qtSwordPlugin/templates/depositSuccess.xml.php
  Tue Oct 11 17:55:18 2011        (r10024)
@@ -2,8 +2,10 @@
 <entry xmlns="http://www.w3.org/2005/Atom";
        xmlns:sword="http://purl.org/net/sword/";>
 
+  <!-- Deposit #id -->
   <title>My Deposit</title>
 
+  <!-- id / date -->
   <id>info:something:1</id>
 
   <updated>2008-08-18T14:27:08Z</updated>
@@ -19,20 +21,24 @@
     */
   ?>
 
-  <summary type="text">A summary</summary>
+  <!-- <category>...<category> -->
 
-  <content type="application/zip" 
src="http://www.myrepository.ac.uk/geography-collection/deposit1.zip"/>
+  <!-- <summary type="text">...</summary> -->
 
-  <link rel="edit" 
href="http://www.myrepository.ac.uk/geography-collection/atom/my_deposit.atom"; 
/>
+  <!-- <content type="application/zip" src="..."/> -->
+
+  <!-- ICA-AtoM 1.2 -->
+  <generator uri="http://dummy-sword-server.example.com/"; 
version="1.3">Stuart's Dummy SWORD Server</generator>
 
+  <link rel="edit" 
href="http://www.myrepository.ac.uk/geography-collection/atom/my_deposit.atom"; 
/>
   <link rel="edit-media" 
href="http://www.myrepository.ac.uk/geography-collection/atom/my_deposit.atom"; 
/>
 
-  <sword:packaging><?php echo $packaging ?></sword:packaging>
+  <sword:noOp>false</sword:noOp>
 
-  <sword:userAgent><?php echo $_SERVER['HTTP_USER_AGENT'] ?></sword:userAgent>
+  <sword:packaging><?php echo $packageFormat ?></sword:packaging>
 
-  <generator uri="http://www.myrepository.ac.uk/engine"; version="1.0"/>
+  <sword:userAgent><?php echo $_SERVER['HTTP_USER_AGENT'] ?></sword:userAgent>
 
-  <sword:treatment>Treatment description</sword:treatment>
+  <!-- <sword:treatment>Treatment description</sword:treatment> -->
 
 </entry>

Added: 
trunk/plugins/qtSwordPlugin/modules/qtSwordPlugin/templates/error/ErrorMaxUploadSizeExceeded.xml.php
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ 
trunk/plugins/qtSwordPlugin/modules/qtSwordPlugin/templates/error/ErrorMaxUploadSizeExceeded.xml.php
        Tue Oct 11 17:55:18 2011        (r10024)
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<sword:error
+  href="http://purl.org/net/sword/error/MAX_UPLOAD_SIZE_EXCEEDED";
+  xmlns:atom="http://www.w3.org/2005/Atom";
+  xmlns:sword="http://purl.org/net/sword/";>
+   <atom:summary type="text">The uploaded file exceeded the maximum file size 
this server will accept (the file is 2089kB but the server will only accept 
files as large as 2048kB)</atom:summary>
+   <atom:title type="text">ERROR</atom:title>
+   <atom:updated>2011-10-11T22:06:00.434Z</atom:updated>
+   <sword:userAgent>CASIS Test Client</sword:userAgent>
+</sword:error>

Modified: 
trunk/plugins/qtSwordPlugin/modules/qtSwordPlugin/templates/servicedocumentSuccess.xml.php
==============================================================================
--- 
trunk/plugins/qtSwordPlugin/modules/qtSwordPlugin/templates/servicedocumentSuccess.xml.php
  Tue Oct 11 15:17:33 2011        (r10023)
+++ 
trunk/plugins/qtSwordPlugin/modules/qtSwordPlugin/templates/servicedocumentSuccess.xml.php
  Tue Oct 11 17:55:18 2011        (r10024)
@@ -19,18 +19,18 @@
         <atom:title type="text"><?php echo render_title($item) ?></atom:title>
 
         <!-- Accepted media ranges -->
-        <?php foreach (qtSwordPluginConfiguration::$mediaRanges as $item): ?>
-          <accept><?php echo $item ?></accept>
+        <?php foreach (qtSwordPluginConfiguration::$mediaRanges as 
$mediaRange): ?>
+          <accept><?php echo $mediaRange ?></accept>
         <?php endforeach; ?>
 
         <!-- MAY be included. Used for a human-readable description of 
collection policy. Include either a text description or a URI. --> 
-        <sword:collectionPolicy>No guarantee of service, or that deposits will 
be retained for any length of time.</sword:collectionPolicy>
+        <!-- <sword:collectionPolicy>No guarantee of service, or that deposits 
will be retained for any length of time.</sword:collectionPolicy> -->
 
         <!-- SHOULD be included. Used to indicate if mediated deposit is 
allowed on the defined collection. -->
         <sword:mediation><?php echo $mediation ?></sword:mediation>
 
         <!-- MAY be included. Used for a human-readable statement about what 
treatment the deposited resource will receive. -->
-        <sword:treatment>This is a server</sword:treatment>
+        <!-- <sword:treatment>This is a server</sword:treatment> -->
 
         <!-- MAY be included. Used to identify the content packaging types 
supported by this collection. SHOULD be a URI from [SWORD-TYPES]. The q 
attribute MAY be used to indicate relative preferences between packaging 
formats (See Part A Section 1.1). -->
         <?php foreach (qtSwordPluginConfiguration::$packaging as $key => 
$value): ?>
@@ -43,7 +43,7 @@
         <?php endif; ?>
 
         <!-- The use of a Dublin Core dcterms:abstract element containing a 
description of the Collection is RECOMMENDED. -->
-        <dcterms:abstract>Collection description</dcterms:abstract>
+        <!-- <dcterms:abstract>Collection description</dcterms:abstract> -->
 
       </collection>
 

-- 
You received this message because you are subscribed to the Google Groups 
"Qubit Toolkit Commits" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/qubit-commits?hl=en.

Reply via email to