Пример #1
0
 /**
  * Delete the file and all associated rows (done by foreign keys) and files
  * $params object should be a json like
  * <code>
  * {
  *  'id': 'xxxx-x-x-x-x-x-x'
  * }
  * </code>
  *
  * @param $params
  * @return RokCommon_Ajax_Result
  */
 public function file($params)
 {
     $result = new RokCommon_Ajax_Result();
     $tx = RokGallery_Doctrine::getConnection()->transaction;
     $tx->setIsolation('READ UNCOMMITTED');
     try {
         if (count($_FILES) == 0) {
             throw new RokGallery_Job_Exception(rc__('ROKGALLERY_NO_FILES_SENT'));
         }
         $job = RokGallery_Job::get($params->id);
         if ($job === false) {
             throw new RokCommon_Ajax_Exception(rc__('ROKGALLERY_UNABLE_TO_FIND_JOB', $params->id));
         }
         if ($job->getStateName() != RokGallery_Job::STATE_PREPPING) {
             throw new RokGallery_Job_Exception(rc__('ROKGALLERY_NOT_IN_PREPPING_STATUS'));
         }
         if ($job->getType() != RokGallery_Job::TYPE_IMPORT) {
             throw new RokGallery_Job_Exception(rc__('ROKGALLERY_NOT_AN_IMPORT_JOB'));
         }
         $job_properties = $job->getProperties();
         if (empty($job_properties)) {
             $job_properties = array();
         }
         $basepath = RokGallery_Config::getOption(RokGallery_Config::OPTION_JOB_QUEUE_PATH) . DS . $job->getId();
         if (!file_exists($basepath)) {
             @mkdir($basepath);
             RokGallery_Queue_DirectoryCreate::add($basepath);
         }
         if (!(file_exists($basepath) && is_dir($basepath) && is_writable($basepath))) {
             throw new RokGallery_Job_Exception(rc__('ROKGALLERY_UNABLE_TO_CREATE_OR_WRITE_TO_TEMP_DIR', $basepath));
         }
         $tx->beginTransaction();
         foreach ($_FILES as $uploaded_file) {
             if ($uploaded_file['error'] == UPLOAD_ERR_OK) {
                 $file = new RokGallery_Job_Property_ImportFile();
                 $file->setFilename($uploaded_file['name']);
                 $file->setPath($basepath . DS . $file->getId());
                 move_uploaded_file($uploaded_file['tmp_name'], $file->getPath());
                 $job_properties[] = $file;
             }
         }
         $job->setProperties($job_properties);
         $job->save();
         $tx->commit();
     } catch (Exception $e) {
         $tx->rollback();
         throw $e;
     }
     return $result;
 }
Пример #2
0
 /**
  */
 public function process()
 {
     try {
         /** @var $properties RokGallery_Job_Property_ImportFile[] */
         $properties = $this->_job->getProperties();
         $total_files = count($properties['files']);
         foreach ($properties['files'] as $key => &$file) {
             // keep bumping the time as log as a file doesnt take 30 seconds or more
             /** @var RokGallery_Job_Property_ImportFile $file  */
             if (!$this->_checkState($properties, rc__('ROKGALLERY_CREATE_UPDATE'))) {
                 return;
             }
             if ($file->isCompleted()) {
                 continue;
             }
             RokGallery_Doctrine::getConnection()->beginTransaction();
             $gallery = RokGallery_Model_GalleryTable::getSingle($properties['galleryId']);
             if ($gallery === false) {
                 throw new RokGallery_Job_Exception(rc__('ROKGALLERY_NOT_A_VALID_GALLERY'));
             }
             $full_file = RokGallery_Model_FileTable::getSingle($file->getId());
             if ($full_file === false) {
                 $file->setStatus(rc__('ROKGALLERY_UNABLE_TO_FIND_FILE'));
                 $file->setError(true);
                 RokGallery_Doctrine::getConnection()->commit();
                 continue;
             }
             if ($gallery && $full_file) {
                 $full_file->updateSlicesForGallery($gallery);
             }
             $file->setCompleted();
             $percent = (int) (($key + 1) / $total_files * 100);
             $this->_job->setProperties($properties);
             $this->_job->save(rc__('ROKGALLERY_UPDATED_GALLERY_SLICE_FOR_FILE_N', $full_file->title), $percent);
             RokGallery_Doctrine::getConnection()->commit();
         }
         $this->_job->Complete(rc__('ROKGALLERY_GALLERY_UPDATE_COMPLETE'));
         if (RokGallery_Config::getOption(RokGallery_Config::OPTION_AUTO_CLEAR_SUCCESSFUL_JOBS, false)) {
             sleep(5);
             $this->_job->Delete();
         }
         return;
     } catch (Exception $e) {
         RokGallery_Doctrine::getConnection()->rollback();
         $this->_job->Error($e->getMessage());
         return;
     }
 }
Пример #3
0
 /**
  */
 public function process()
 {
     try {
         /** @var $properties RokGallery_Job_Property_FileTags[] */
         $properties = $this->_job->getProperties();
         $total_files = count($properties);
         foreach ($properties as $key => &$tagAddition) {
             // keep bumping the time as log as a file doesnt take 30 seconds or more
             if (!$this->_checkState($properties, rc__('ROKGALLERY_CREATE_UPDATE'))) {
                 return;
             }
             if ($tagAddition->isCompleted()) {
                 continue;
             }
             RokGallery_Doctrine::getConnection()->beginTransaction();
             $file = RokGallery_Model_FileTable::getSingle($tagAddition->getFileId());
             if (!$file) {
                 $tagAddition->setStatus(rc__('ROKGALLERY_UNABLE_TO_FIND_FILE'));
                 $tagAddition->setError(true);
                 RokGallery_Doctrine::getConnection()->commit();
                 continue;
             }
             RokGallery_Model_FileTable::addTagsToFile($file, $tagAddition->getTags());
             $tagAddition->setCompleted();
             $percent = (int) (($key + 1) / $total_files * 100);
             $this->_job->setProperties($properties);
             $this->_job->save(rc__('ROKGALLERY_ADDED_TAGS_TO_FILE_N', $file->title), $percent);
             RokGallery_Doctrine::getConnection()->commit();
         }
         $this->_job->Complete(rc__('ROKGALLERY_TAG_ADDITION_COMPLETE'));
         if (RokGallery_Config::getOption(RokGallery_Config::OPTION_AUTO_CLEAR_SUCCESSFUL_JOBS, false)) {
             sleep(5);
             $this->_job->Delete();
         }
         return;
     } catch (Exception $e) {
         RokGallery_Doctrine::getConnection()->rollback();
         $this->_job->Error($e->getMessage());
         return;
     }
 }
Пример #4
0
 /**
  * create a new Job and return the Job Info
  *
  * @param $params
  * @return RokCommon_Ajax_Result
  */
 public function createJob($params)
 {
     $result = new RokCommon_Ajax_Result();
     $tx = RokGallery_Doctrine::getConnection()->transaction;
     $tx->setIsolation('REPEATABLE READ');
     try {
         $tx->beginTransaction();
         $job = new RokGallery_Model_Job();
         $job->id = RokCommon_UUID::generate();
         $job->type = RokGallery_Model_Job::TYPE_UPLOAD;
         $job->state = RokGallery_Model_Job::STATE_PENDING;
         $job->percent = 0;
         $job->save();
         $result->setPayload(array('job' => $job->id));
         $tx->commit();
     } catch (Exception $e) {
         $tx->rollback();
         throw $e;
     }
     return $result;
 }
Пример #5
0
 public function saveCurrentState($state, $status = null, $percent = 0)
 {
     $tx = RokGallery_Doctrine::getConnection()->transaction;
     $tx->setIsolation('REPEATABLE READ');
     try {
         $tx->beginTransaction();
         $this->_job->state = $state;
         if ($percent != 0) {
             $this->_job->percent = $percent;
         }
         $this->_job->status = $status;
         $this->_job->sm = serialize($this->_fsm);
         $this->_job->save();
         $tx->commit();
     } catch (Exception $e) {
         $tx->rollback();
         throw $e;
     }
 }
Пример #6
0
 /**
  */
 public function process()
 {
     try {
         /** @var RokGallery_Job_Property_ImportFile[] $import_files  */
         $import_files = $this->_job->getProperties();
         $total_files = count($import_files);
         foreach ($import_files as $key => &$import_file) {
             $this->_job->refreshState();
             if (!$this->_checkState($properties, 'Import')) {
                 return;
             }
             if ($import_file->isCompleted()) {
                 continue;
             }
             RokGallery_Doctrine::getConnection()->beginTransaction();
             $file = RokGallery_Model_File::createNew($import_file->getFilename(), $import_file->getPath());
             // If we need to check to make sure it is not a duplicated file
             if (RokGallery_Config::getOption(RokGallery_Config::OPTION_ALLOW_DUPLICATE_FILES, true) == false && RokGallery_Model_FileTable::getMD5($file->md5) !== false && RokGallery_Model_FileTable::getMD5($file->md5)->count() > 0) {
                 throw new RokGallery_Job_Exception(rc__('ROKGALLERY_A_MATCHING_FILE_FOR_N_IN_SYSTEM', $file->filename));
             }
             // Copy file to fine directory
             $basepath = dirname($file->getFullPath());
             if (!file_exists($basepath)) {
                 @mkdir($basepath, 0777, true);
                 RokGallery_Queue_DirectoryCreate::add($basepath);
             }
             if (!(file_exists($basepath) && is_dir($basepath) && is_writable($basepath))) {
                 throw new RokGallery_Job_Exception(rc__('ROKGALLERY_UNABLE_TO_CREATE_OR_WRITE_TO_THE_DIR_N', $basepath));
             }
             // Move the file to its final location
             $endpath = $file->getFullPath();
             rename($import_file->getPath(), $endpath);
             // update the image file info
             $file_image_info = @getimagesize($endpath);
             $file->xsize = $file_image_info[0];
             /// x size
             $file->ysize = $file_image_info[1];
             /// y size
             // Create the initial admin slice
             $this->createInitialAdminSlice($file);
             // Save the file to the db;
             $file->save();
             $import_file->setCompleted();
             $percent = (int) (($key + 1) / $total_files * 100);
             $this->_job->setProperties($import_files);
             $this->_job->save(rc__('ROKGALLERY_IMPORTED_FILE_N', $import_file->getFilename()), $percent);
             RokGallery_Doctrine::getConnection()->commit();
             $file->free(true);
         }
         $this->_job->Complete('Importing Complete');
         if (RokGallery_Config::getOption(RokGallery_Config::OPTION_AUTO_CLEAR_SUCCESSFUL_JOBS, false)) {
             sleep(5);
             $this->_job->Delete();
         }
         return;
     } catch (Exception $e) {
         RokGallery_Doctrine::getConnection()->rollback();
         $this->_job->Error($e->getMessage());
         return;
     }
 }
Пример #7
0
 /**
  * Wipes all tags from a  {@link RokGallery_Model_File} object
  * $params object should be a json like
  * <code>
  * {
  *  'id': 1
  * }
  * </code>
  *
  * @param  $params
  * @return RokCommon_Ajax_Result
  */
 public function wipeTags($params)
 {
     $result = new RokCommon_Ajax_Result();
     try {
         RokGallery_Doctrine::getConnection()->beginTransaction();
         $query = Doctrine_Query::create()->delete('RokGallery_Model_FileTags ft')->where('ft.file_id = ?', $params->id);
         $query->execute();
         RokGallery_Doctrine::getConnection()->commit();
     } catch (Exception $e) {
         RokGallery_Doctrine::getConnection()->rollback();
         throw $e;
     }
     return $result;
 }
Пример #8
0
 /**
  * Delete the slice and all associated rows (done by foreign keys) and files
  * $params object should be a json like
  * <code>
  * {
  *  'id': 1
  * }
  * </code>
  *
  * @param $params
  * @return RokCommon_Ajax_Result
  */
 public function delete($params)
 {
     $result = new RokCommon_Ajax_Result();
     try {
         RokGallery_Doctrine::getConnection()->beginTransaction();
         /** @var $slice RokGallery_Model_Slice */
         $slice = RokGallery_Model_SliceTable::getSingle($params->id);
         if ($slice === false) {
             throw new RokCommon_Ajax_Exception(rc__('ROKGALLERY_UNABLE_TO_FIND_SLICE_N', $params->id));
         }
         if ($slice->admin_thumb) {
             throw new RokCommon_Ajax_Exception(rc__('ROKGALLERY_UNABLE_TO_DELETE_ADMIN_SLICE'));
         }
         $slice->delete();
         RokGallery_Doctrine::getConnection()->commit();
     } catch (Exception $e) {
         RokGallery_Doctrine::getConnection()->rollback();
         throw $e;
     }
     return $result;
 }
Пример #9
0
 /**
  * Get the full list of jobs
  * <code>
  * {
  *   'ids': [1,2,3],
  *   'tags': ['tag1','tag2']
  * }
  * </code>
  * @param $params
  * @return RokCommon_Ajax_Result
  */
 public function removeTags($params)
 {
     $result = new RokCommon_Ajax_Result();
     try {
         try {
             RokGallery_Doctrine::getConnection()->beginTransaction();
             $properties = array();
             foreach ($params->ids as $file_id) {
                 $properties[] = new RokGallery_Job_Property_FileTags($file_id, $params->tags);
             }
             $job = RokGallery_Job::create(RokGallery_Job::TYPE_TAGREMOVAL);
             $job->setProperties($properties);
             $job->save();
             RokGallery_Doctrine::getConnection()->commit();
             // Disconnect and process job
             $this->sendDisconnectingReturn($result);
             $job->Ready();
             $job->Run('Starting Tag Removals');
             $job->process();
             die;
         } catch (Exception $e) {
             RokGallery_Doctrine::getConnection()->rollback();
             throw $e;
         }
     } catch (Exception $e) {
         throw $e;
     }
 }
Пример #10
0
 /**
  * Save the order of images in a gallery
  * $params object should be a json like
  * <code>
  * {
  *   "id" : 1,
  *   "order": [1, 2, 10, 3, 8]
  * }
  * </code>
  * @param $params
  */
 public function order($params)
 {
     $result = new RokCommon_Ajax_Result();
     $tx = RokGallery_Doctrine::getConnection()->transaction;
     $tx->setIsolation('REPEATABLE READ');
     try {
         $tx->beginTransaction();
         $gallery = RokGallery_Model_GalleryTable::getSingle($params->id);
         if ($gallery === false) {
             throw new RokCommon_Ajax_Exception('No gallery with id ' . $params->id);
         }
         $gallery->setSliceOrder($params->order);
         $tx->commit();
     } catch (Exception $e) {
         $tx->rollback();
         throw $e;
     }
     return $result;
 }
Пример #11
0
 /**
  * Update the profile
  * $params object should be a json like
  * <code>
  * {
  *  'id': 1,
  *  'profile':{'name':'new name','description':'new description'}
  * }
  * </code>
  *
  * @param $params
  * @return RokCommon_Ajax_Result
  */
 public function update($params)
 {
     $result = new RokCommon_Ajax_Result();
     try {
         RokGallery_Doctrine::getConnection()->beginTransaction();
         $profile = Doctrine_Core::getTable('RokGallery_Model_Profile')->getSingle($params->id);
         if (array_key_exists('profile', $params->profile)) {
             $params->profile['profile'] = json_encode($params->profile['profile']);
         }
         foreach ($params->file as $field => $value) {
             if (isset($profile->{$field})) {
                 $profile->{$field} = $value;
             }
         }
         $profile->save();
         RokGallery_Doctrine::getConnection()->commit();
     } catch (Exception $e) {
         RokGallery_Doctrine::getConnection()->rollback();
         throw $e;
     }
     return $result;
 }