Пример #1
0
 /**
  * Run the database seeds.
  *
  * @return void
  */
 public function run()
 {
     $uploadPoints = array(array("id" => 1, "description" => "For side banner images.", "fileTypeId" => 1), array("id" => 2, "description" => "For cover images.", "fileTypeId" => 2), array("id" => 3, "description" => "For vod video uploads.", "fileTypeId" => 3), array("id" => 4, "description" => "For cover art uploads.", "fileTypeId" => 4), array("id" => 5, "description" => "For side banner fill images.", "fileTypeId" => 10));
     foreach ($uploadPoints as $b => $a) {
         $fileType = FileType::find($a['fileTypeId']);
         unset($a['fileTypeId']);
         $p = UploadPoint::find($a['id']);
         if (is_null($p)) {
             $p = new UploadPoint($a);
             $p->fileType()->associate($fileType);
         } else {
             $p->fileType()->associate($fileType);
         }
         $p->save();
     }
     $this->command->info('Upload points created/updated!');
 }
Пример #2
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.');
 }
Пример #3
0
 public static function getUploadPointExtensions($uploadPointId)
 {
     if (in_array($uploadPointId, self::$uploadPointExtensionsCache)) {
         return self::$uploadPointExtensionsCache[$uploadPointId];
     }
     $a = UploadPoint::with("fileType", "fileType.extensions")->findOrFail($uploadPointId);
     $extensions = $a->fileType->getExtensionsArray();
     // add to cache
     self::$uploadPointExtensionsCache[$a->id] = $extensions;
     return $extensions;
 }
Пример #4
0
 public static function register($uploadPointId, $fileId, File $fileToReplace = null)
 {
     $uploadPoint = UploadPoint::with("fileType", "fileType.extensions")->find($uploadPointId);
     if (is_null($uploadPoint)) {
         throw new Exception("Invalid upload point.");
     }
     if (!is_null($fileToReplace) && !is_null($fileId) && intval($fileToReplace->id, 10) === intval($fileId, 10)) {
         // if we are replacing the file with the same file then nothing to do.
         // just return the model
         return $fileToReplace;
     }
     $file = null;
     if (!is_null($fileId)) {
         $fileId = intval($fileId, 10);
         $file = File::with("uploadPoint")->find($fileId);
         if (is_null($file)) {
             throw new Exception("File model could not be found.");
         } else {
             if (is_null($file->uploadPoint)) {
                 throw new Exception("This file doesn't have an upload point. This probably means it was created externally and 'register' should not be used on it.");
             } else {
                 if ($file->in_use) {
                     throw new Exception("This file has already been registered.");
                 } else {
                     if ($file->uploadPoint->id !== $uploadPoint->id) {
                         throw new Exception("Upload points don't match. This could happen if a file was uploaded at one upload point and now the file with that id is being registered somewhere else.");
                     }
                 }
             }
         }
     }
     if (!is_null($file)) {
         $file->in_use = true;
         // mark file as being in_use now
     }
     DB::transaction(function () use(&$file, &$fileToReplace) {
         $oldIds = array();
         // if the old file has old file ids that used to point to it then save them so they can be copied across
         if (!is_null($fileToReplace)) {
             $oldIds = $fileToReplace->oldFileIds()->get()->map(function ($a) {
                 return intval($a->old_file_id);
             });
             $oldIds[] = intval($fileToReplace->id);
         }
         // this must happen before new file is saved (after the old ids that pointed to this file have been retrieved)
         // so that don't end up with duplicate old file ids
         if (!is_null($fileToReplace)) {
             self::delete($fileToReplace);
         }
         if (!is_null($file)) {
             foreach ($oldIds as $a) {
                 $file->oldFileIds()->save(new OldFileId(array("old_file_id" => $a)));
             }
             if ($file->save() === false) {
                 throw new Exception("Error saving file model.");
             }
         }
     });
     return $file;
 }