예제 #1
0
 /**
  * Run the database seeds.
  *
  * @return void
  */
 public function run()
 {
     // make sure there is at least one file entry that other seeders can use
     if (File::where("in_use", "=", true)->count() === 0) {
         $file = new File(array("in_use" => true, "size" => rand(10, 9999)));
         $file->fileType()->associate(FileType::first());
         $file->uploadPoint()->associate(UploadPoint::first());
         $file->save();
     }
     $this->command->info('File record created.');
 }
예제 #2
0
 public function process($allowedIds = null)
 {
     if ($this->processCalled) {
         throw new Exception("'process' can only be called once.");
     }
     $this->processCalled = true;
     $this->responseData = array("success" => false);
     $success = false;
     $info = $this->buildFile();
     if (!$info['success']) {
         $success = false;
         $this->responseData['success'] = false;
         $this->responseData['wasChunk'] = true;
     } else {
         if (is_null($info['info'])) {
             $success = true;
             $this->responseData['success'] = true;
             $this->responseData['wasChunk'] = true;
         } else {
             $fileInfo = $info['info'];
             $uploadPointId = FormHelpers::getValue("upload_point_id");
             if (Csrf::hasValidToken() && !is_null($uploadPointId) && (is_null($allowedIds) || in_array($uploadPointId, $allowedIds, true))) {
                 $uploadPointId = intval($uploadPointId, 10);
                 $uploadPoint = UploadPoint::with("fileType", "fileType.extensions")->find($uploadPointId);
                 if (!is_null($uploadPoint) && strlen($fileInfo['name']) <= self::$maxFileLength) {
                     $fileLocation = $fileInfo['path'];
                     $fileName = $fileInfo['name'];
                     $fileSize = FileHelpers::filesize64($fileLocation);
                     $extension = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
                     $extensions = array();
                     $extensionModels = $uploadPoint->fileType->extensions;
                     if (!is_null($extensionModels)) {
                         foreach ($extensionModels as $a) {
                             $extensions[] = $a->extension;
                         }
                     }
                     if (in_array($extension, $extensions) && $fileSize != FALSE && $fileSize > 0) {
                         try {
                             DB::beginTransaction();
                             // create the file reference in the db
                             $fileDb = new File(array("in_use" => false, "filename" => $fileName, "size" => $fileSize, "session_id" => Session::getId()));
                             $fileDb->fileType()->associate($uploadPoint->fileType);
                             $fileDb->uploadPoint()->associate($uploadPoint);
                             if ($fileDb->save() !== FALSE) {
                                 // commit transaction so file record is committed to database
                                 DB::commit();
                                 DB::beginTransaction();
                                 // another transaction to make sure the session doesn't become null on the model (which would result in the upload processor trying to delete it, and failing silently if it can't find the file) whilst the file is being moved.
                                 $fileDb = File::find($fileDb->id);
                                 if (is_null($fileDb)) {
                                     throw new Exception("File model has been deleted!");
                                 }
                                 if ($fileDb->session_id !== Session::getId()) {
                                     throw new Exception("Session has changed between transactions!");
                                 }
                                 // move the file providing the file record created successfully.
                                 // it is important there's always a file record for each file.
                                 if (self::moveFile($fileLocation, Config::get("custom.files_location") . DIRECTORY_SEPARATOR . $fileDb->id)) {
                                     // set ready_for_processing to true so that processing can start.
                                     $fileDb->ready_for_processing = true;
                                     $fileDb->save();
                                     DB::commit();
                                     // if there is a failure before the ready_for_processing flag is set then it is possible for there to either be a file which will never be removed automatically, or no file for this record. I think this is the only place in the entire system where there could be an error which would require manual attention.
                                     // success
                                     $success = true;
                                     $this->responseData['success'] = true;
                                     $this->responseData['id'] = $fileDb->id;
                                     $this->responseData['fileName'] = $fileName;
                                     $this->responseData['fileSize'] = $fileSize;
                                     $this->responseData['processInfo'] = $fileDb->getProcessInfo();
                                 } else {
                                     DB::rollback();
                                 }
                             } else {
                                 DB::rollback();
                             }
                         } catch (\Exception $e) {
                             DB::rollback();
                             throw $e;
                         }
                     }
                 }
             }
         }
     }
     return $success;
 }