/** * Post Upload a new File * @param type $fileIndex */ public function postUploadFile($fileIndex = 'file', $caption = null) { try { if (!$this->postFileEnabled()) { throw new \Zbase\Exceptions\ConfigNotFoundException('Uploading is disabled for ' . $this->postTableName()); } $defaultImageFormat = zbase_config_get('post.files.image.format', 'png'); $folder = $this->postFileUploadFolder(); zbase_directory_check($folder, true); /** * Check if we have a URL given * @TODO save image that are from remote URL */ if (preg_match('/http\\:/', $fileIndex) || preg_match('/https\\:/', $fileIndex)) { return; } if (!empty($_FILES[$fileIndex]['name'])) { $filename = zbase_file_name_from_file($_FILES[$fileIndex]['name'], time(), true); $uploadedFile = zbase_file_upload_image($fileIndex, $folder, $filename, $defaultImageFormat); } if (!empty($uploadedFile) && zbase_file_exists($uploadedFile)) { /** * No Table, let's use the option column * to save the image link */ if (!$this->postFileHasTable()) { $file = new \stdClass(); } if (!empty($file)) { $file->is_primary = 0; $file->status = 2; $file->mimetype = zbase_file_mime_type($uploadedFile); $file->size = zbase_file_size($uploadedFile); $file->filename = basename($uploadedFile); $file->post_id = $this->postId(); $file->caption = $caption; return $this->postFileAdd($file); } } } catch (\Zbase\Exceptions\RuntimeException $e) { zbase_exception_throw($e); return false; } }
/** * Upload a file for this node * @param string $index The Upload file name/index or the URL to file to download and save * @return void */ public function uploadNodeFile($index = 'file') { try { $defaultImageFormat = zbase_config_get('node.files.image.format', 'png'); $folder = zbase_storage_path() . '/' . zbase_tag() . '/' . static::$nodeNamePrefix . '/' . $this->id() . '/'; zbase_directory_check($folder, true); $nodeFileObject = zbase_entity(static::$nodeNamePrefix . '_files', [], true); $nodeFiles = $this->files()->get(); if (preg_match('/http\\:/', $index) || preg_match('/https\\:/', $index)) { // File given is a URL if ($nodeFileObject->isUrlToFile()) { $filename = zbase_file_name_from_file(basename($index), time(), true); $uploadedFile = zbase_file_download_from_url($index, $folder . $filename); } else { $nodeFiles = $this->files()->get(); $nodeFileObject->is_primary = empty($nodeFiles) ? 1 : 0; $nodeFileObject->status = 2; $nodeFileObject->mimetype = null; $nodeFileObject->size = null; $nodeFileObject->filename = null; $nodeFileObject->url = $index; $nodeFileObject->node_id = $this->id(); $nodeFileObject->save(); $this->files()->save($nodeFileObject); return $nodeFileObject; } } if (zbase_file_exists($index)) { $uploadedFile = $index; $filename = basename($index); } if (!empty($_FILES[$index]['name'])) { $filename = zbase_file_name_from_file($_FILES[$index]['name'], time(), true); $uploadedFile = zbase_file_upload_image($index, $folder, $filename, $defaultImageFormat); } if (!empty($uploadedFile) && zbase_file_exists($uploadedFile)) { $nodeFileObject->is_primary = empty($nodeFiles) ? 1 : 0; $nodeFileObject->status = 2; $nodeFileObject->mimetype = zbase_file_mime_type($uploadedFile); $nodeFileObject->size = zbase_file_size($uploadedFile); $nodeFileObject->filename = basename($uploadedFile); $nodeFileObject->node_id = $this->id(); $nodeFileObject->save(); $this->files()->save($nodeFileObject); return $nodeFileObject; } } catch (\Zbase\Exceptions\RuntimeException $e) { if (zbase_is_dev()) { dd($e); } zbase_abort(500); } }
/** * Receive the File/Image * * @param \Zbase\Entity\Laravel\Entity $parentObject The Parent */ public function receiveFile(\Zbase\Entity\Laravel\Entity $parentObject) { try { $index = 'file'; $entityName = $this->entityName; $defaultImageFormat = zbase_config_get('node.files.image.format', 'png'); $folder = zbase_storage_path() . '/' . zbase_tag() . '/' . $this->actionUrlRouteName() . '/' . $parentObject->id() . '/'; zbase_directory_check($folder, true); $nodeFileObject = zbase_entity($entityName, [], true); $nodeFiles = $parentObject->childrenFiles(); if (preg_match('/http\\:/', $index) || preg_match('/https\\:/', $index)) { // File given is a URL if ($nodeFileObject->isUrlToFile()) { $filename = zbase_file_name_from_file(basename($index), time(), true); $uploadedFile = zbase_file_download_from_url($index, $folder . $filename); } else { $this->is_primary = empty($nodeFiles) ? 1 : 0; $this->status = 2; $this->mimetype = null; $this->size = null; $this->filename = null; $this->url = $index; $this->{$this->parentObjectIndexId} = $parentObject->id(); $this->user_id = zbase_auth_has() ? zbase_auth_user()->id() : null; $this->save(); return true; } } if (zbase_file_exists($index)) { $uploadedFile = $index; $filename = basename($index); } if (!empty($_FILES[$index]['name'])) { $filename = zbase_file_name_from_file($_FILES[$index]['name'], time(), true); $uploadedFile = zbase_file_upload_image($index, $folder, $filename, $defaultImageFormat); } if (!empty($uploadedFile) && zbase_file_exists($uploadedFile)) { $this->is_primary = empty($nodeFiles) ? 1 : 0; $this->status = 2; $this->user_id = zbase_auth_has() ? zbase_auth_user()->id() : null; $this->mimetype = zbase_file_mime_type($uploadedFile); $this->size = zbase_file_size($uploadedFile); $this->filename = basename($uploadedFile); $this->{$this->parentObjectIndexId} = $parentObject->id(); if (empty($nodeFiles)) { $parentObject->image = $this->filename; $parentObject->save(); } $this->save(); return true; } } catch (\Zbase\Exceptions\RuntimeException $e) { if (zbase_is_dev()) { dd($e); } zbase_abort(500); } return false; }