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; }
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; }