DataObjects/core.sql
[Pman.Core] / DataObjects / Images.php
index c98c4c4..56285fc 100644 (file)
@@ -56,13 +56,25 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
     
     function beforeInsert($q, $roo) 
     {
-        print_r($q);
         if (isset($q['_remote_upload'])) {
-            //$fn = $this->remoteUpload($roo, $q['_remote_upload']);
+            require_once 'System.php';
+            $tmpdir  = System::mktemp("-d remote_upload");
             
-            // load the file..
-            $fn = file_get_contents($q['_remote_upload']);
-            $this->createFrom($fn);
+            $path = $tmpdir . '/' . basename($q['_remote_upload']);
+            if(!file_exists($path)){
+               file_put_contents($path, file_get_contents($q['_remote_upload'])); 
+            }
+            
+            $imageInfo = getimagesize($path);
+            require_once 'File/MimeType.php';
+            $y = new File_MimeType();
+            $ext = $y->toExt(trim((string) $imageInfo['mime'] ));
+            
+            if (!preg_match("/\." . $ext."$/", $path, $matches)) {
+                rename($path,$path.$ext);
+            }
+            
+            $this->createFrom($path);
             
             $roo->addEvent("ADD", $this, $this->toEventString());
         
@@ -71,7 +83,7 @@ class Pman_Core_DataObjects_Images extends DB_DataObject
             $roo->loadMap($r);
             $r->limit(1);
             $r->find(true);
-            $roo->jok($r->toArray());
+            $roo->jok($r->URL(-1,'/Images') . '#attachment-'.  $r->id);
         }
         
     }