Ejemplo n.º 1
0
 /**
  * Get provider by name
  *
  * @param string $name
  * @return ModelContentProvider|null
  */
 public static function get($name)
 {
     $item = Memcache::getInstance()->put('cdn_' . $name, 60 * 60 * 24, function () use($name) {
         return ModelContentProvider::fetchOne('SELECT * FROM {table} WHERE `name` = %s', $name);
     });
     return $item->hasRow() ? $item : null;
 }
Ejemplo n.º 2
0
 public function __construct()
 {
     parent::__construct();
     $this->contentProviders = ModelContentProvider::all();
     if ($this->input('organisation')) {
         $this->organisations = ModelOrganisation::getByQuery($this->input('organisation'), 20, 0);
     }
 }
Ejemplo n.º 3
0
 public function getLocalDirectory()
 {
     if ($this->cdn) {
         $cdn = ModelContentProvider::get($this->cdn);
         return rtrim($cdn->path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $this->source_id . DIRECTORY_SEPARATOR . $this->path;
     }
     return $this->path;
 }
Ejemplo n.º 4
0
 protected function saveThumbnail($filename, $contents, $size)
 {
     $path = dirname($filename);
     $path = $path === '.' || empty($path) ? '' : rtrim($path, '/') . '/';
     $name = basename($filename);
     $name = substr($name, 0, strripos($name, '.'));
     $cdn = ModelContentProvider::getRandomActive();
     $this->thumbnail = new ModelThumbnail();
     $this->thumbnail->cdn = $cdn->name;
     $this->thumbnail->identifier = $this->getIdentifier();
     $this->thumbnail->path = $path;
     $this->thumbnail->source_id = request()->source->id;
     $this->thumbnail->name = $this->getIdentifier() . '.' . $this->format;
     $this->thumbnail->original_name = $name . '.' . $this->format;
     $this->thumbnail->mime = $this->image->mime();
     if (!is_dir($this->thumbnail->getLocalDirectory())) {
         mkdir($this->thumbnail->getLocalDirectory(), 0777, true);
     }
     file_put_contents($this->thumbnail->getLocalFile(), $contents);
     $this->thumbnail->size = $size;
     $this->thumbnail->save();
 }
Ejemplo n.º 5
0
// -- CLEANUP THUMBNAILS ---
echo chr(10) . '  [ CLEANING UNUSED THUMBNAILS ]' . chr(10) . chr(10);
$thumbnails = \NinjaImg\Model\ModelThumbnail::getByMaxAge(THUMBNAIL_MAX_AGE_DAYS);
if (!$thumbnails->hasRows()) {
    echo '   - No thumbnails found';
} else {
    /* @var $thumbnail \NinjaImg\Model\ModelThumbnail */
    foreach ($thumbnails->getRows() as $thumbnail) {
        echo '   - Deleting thumbnail: ' . $thumbnail->name . chr(10);
        $thumbnail->delete();
    }
}
echo chr(10) . chr(10);
// DETECT DISKSPACE CHANGES
echo '  [ CHECK CONTENT-PROVIDER DISKSPACE ]' . chr(10) . chr(10);
foreach (\NinjaImg\Model\ModelContentProvider::all(true) as $cdn) {
    echo '   + Checking ' . $cdn->name . chr(10);
    $total = $cdn->capacity_max_gb && $cdn->capacity_max_gb > 0 ? $cdn->capacity_max_gb : round(disk_total_space($cdn->path) / 1024 / 1024 / 1024);
    $capacity = round(disk_free_space($cdn->path) / 1024 / 1024 / 1024);
    $leftPercentage = round($capacity / $total * 100);
    $lastRun = isset($settings['last_run']) ? $settings['last_run'] : 'Not available';
    /**
     * Send warning if the storage left is <= NOTIFY_MIN_SPACE_PERCENTAGE
     * OTHERWISE
     * send notification if change is > NOTIFY_PERCENTAGE_CHANGE
     */
    $cdn->active = true;
    if ($leftPercentage <= NOTIFY_MIN_SPACE_PERCENTAGE) {
        echo '      - [WARNING] Storage critical: ' . $cdn->capacity_free_gb . '/' . $total . 'GB left' . chr(10);
        mailAdmins('WARNING: STORAGE ALMOST EMPTY ' . $cdn->name, "Yo, bitches!\n\nWARNING: content-provider \"{$cdn->name}\" has almost exceeded it's disk space and has disabled automatically.\n\nLast recorded date:         {$lastRun}\nLast recorded diskspace:    " . round($cdn->capacity_free_gb) . "GB\nDiskspace available:        " . round($capacity) . "GB\nDiskspace limit:            " . round($total) . "GB\n\n- The Ninja Robot");
        $cdn->active = false;