Exemple #1
0
 public function importInfinityPostAttachments(Post $model, Board &$board, $post)
 {
     $post_id = $model->post_id;
     if (!$post_id) {
         return 0;
     }
     $attachments = @json_decode($post->files, true);
     if (!is_array($attachments)) {
         return 0;
     }
     $aModels = [];
     foreach ($attachments as $aIndex => $attachment) {
         if (isset($attachment['error']) && $attachment['error']) {
             continue;
         }
         if (!isset($attachment['file_path']) || !isset($attachment['thumb_path'])) {
             continue;
         }
         $storage = null;
         $path = "{$this->targetLocation}/{$attachment['file_path']}";
         $thumb = "{$this->targetLocation}/{$attachment['thumb_path']}";
         if (file_exists($path)) {
             if (!isset($attachment['type'])) {
                 continue;
             }
             if (!isset($attachment['hash'])) {
                 $attachment['hash'] = md5(file_get_contents($path));
             }
             $storage = FileStorage::getHash($attachment['hash']);
             if (!$storage || (!file_exists($storage->getFullPath()) || !file_exists($storage->getFullPathThumb()))) {
                 $height = null;
                 $width = null;
                 if (isset($attachment['width']) && isset($attachment['height'])) {
                     $height = $attachment['height'];
                     $width = $attachment['width'];
                 }
                 if (!$storage) {
                     $storage = new FileStorage(['hash' => $attachment['hash'], 'banned' => false, 'filesize' => $attachment['size'], 'file_width' => $width, 'file_height' => $height, 'mime' => $attachment['type'], 'meta' => null, 'first_uploaded_at' => Carbon::now(), 'last_uploaded_at' => Carbon::now(), 'upload_count' => 1]);
                 }
                 Storage::makeDirectory($storage->getDirectory());
                 if (!file_exists($storage->getFullPath())) {
                     if (is_link($storage->getFullPath())) {
                         unlink($storage->getFullPath());
                     }
                     symlink($path, $storage->getFullPath());
                 }
                 if ($attachment['thumbwidth'] && file_exists($thumb)) {
                     $storage->has_thumbnail = true;
                     $storage->thumbnail_width = $attachment['thumbwidth'];
                     $storage->thumbnail_height = $attachment['thumbheight'];
                     Storage::makeDirectory($storage->getDirectoryThumb());
                     if (!file_exists($storage->getFullPathThumb())) {
                         if (is_link($storage->getFullPathThumb())) {
                             unlink($storage->getFullPathThumb());
                         }
                         symlink($thumb, $storage->getFullPathThumb());
                     }
                 }
                 $storage->save();
             }
             if ($storage && $storage->exists) {
                 $aModel = ['post_id' => $post_id, 'file_id' => $storage->file_id, 'filename' => $attachment['filename'], 'is_spoiler' => false, 'is_deleted' => false, 'position' => $aIndex];
                 $aModels[] = $aModel;
             } else {
                 ++$skips;
             }
         }
     }
     FileAttachment::insert($aModels);
     return count($aModels);
 }