2018-10-16 16:28:42 +00:00
|
|
|
<?php
|
|
|
|
defined('BASEPATH') OR exit('No direct script access allowed');
|
|
|
|
|
|
|
|
use Coduo\PHPHumanizer\NumberHumanizer;
|
|
|
|
|
|
|
|
class FileModel extends CI_Model
|
|
|
|
{
|
|
|
|
|
|
|
|
function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
2018-12-30 17:52:30 +00:00
|
|
|
private function getPath($fileName, $user) {
|
|
|
|
return 'files/' . ($user != null ? 'userContent/' . $user . '/' : '') . $fileName;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function addToDatabase($name, $originalName, $type, $size, $path, $user) {
|
|
|
|
$userID = NULL;
|
|
|
|
if(is_string($user)) {
|
|
|
|
$user = $this->db->query('SELECT ID FROM users WHERE username = lower(?)', [$user])->result_array();
|
|
|
|
if(!empty($user)) {
|
|
|
|
$userID = $user[0]['ID'];
|
|
|
|
}
|
|
|
|
} else if(is_integer($user)) {
|
|
|
|
$userID = $user;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->db->query('INSERT INTO files (name, original_name, type, size, path, user) VALUES (?, ?, ?, ?, ?, ?)', [$name, $originalName, $type, $size, $path, $userID]);
|
|
|
|
|
|
|
|
$this->db->cache_delete('admin', 'files');
|
2018-10-27 10:08:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function uploadFile($original_name, $tmpname, $size, $type, $userContent = true)
|
2018-10-16 16:28:42 +00:00
|
|
|
{
|
2018-12-26 17:19:28 +00:00
|
|
|
$target_dir = 'files' . DIRECTORY_SEPARATOR . ($userContent ? 'userContent' . DIRECTORY_SEPARATOR : '');
|
2018-10-16 16:28:42 +00:00
|
|
|
$filetype = pathinfo(basename($original_name), PATHINFO_EXTENSION);
|
2018-12-26 17:19:28 +00:00
|
|
|
$name = $this->generateName();
|
|
|
|
$target_file = $target_dir . $name . '.' . $filetype;
|
2018-10-16 16:28:42 +00:00
|
|
|
|
|
|
|
if (!move_uploaded_file($tmpname, $target_file)) {
|
|
|
|
die('File couldn\'t be uploaded!');
|
|
|
|
}
|
|
|
|
|
2018-12-26 17:19:28 +00:00
|
|
|
$target_file = str_replace('\\', '/', $target_file);
|
|
|
|
|
2018-12-30 17:52:30 +00:00
|
|
|
$this->addToDatabase($name, $original_name, $type, $size, $target_file, $userContent);
|
2018-12-26 17:19:28 +00:00
|
|
|
|
|
|
|
echo shell_exec('python /var/www/codeigniter/duplicates.py');
|
2018-11-01 15:09:22 +00:00
|
|
|
|
2018-10-27 10:08:54 +00:00
|
|
|
return "/f/" . $name;
|
2018-10-16 16:28:42 +00:00
|
|
|
}
|
|
|
|
|
2018-12-30 17:52:30 +00:00
|
|
|
public function uploadVideo($name, $max_size, $originalName, $max_width, $user = null) {
|
|
|
|
$path = '.' . DIRECTORY_SEPARATOR . 'files' . DIRECTORY_SEPARATOR;
|
|
|
|
if($user !== null) {
|
|
|
|
$path .= 'userContent' . DIRECTORY_SEPARATOR . $user . DIRECTORY_SEPARATOR;
|
|
|
|
}
|
|
|
|
$config['upload_path'] = $path;
|
|
|
|
|
|
|
|
if(!file_exists($path)) {
|
|
|
|
mkdir($path, 0777);
|
|
|
|
}
|
|
|
|
|
|
|
|
$config['allowed_types'] = 'mp4';
|
|
|
|
$config['max_size'] = $max_size;
|
|
|
|
$config['file_name'] = $this->generateName() . '.' . pathinfo(basename($originalName), PATHINFO_EXTENSION);
|
|
|
|
|
|
|
|
$this->load->library('upload', $config);
|
|
|
|
|
|
|
|
if(!$this->upload->do_upload($name)) {
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
$data = $this->upload->data();
|
|
|
|
|
|
|
|
$this->upload->display_errors();
|
|
|
|
|
|
|
|
$this->addToDatabase($data['raw_name'], $originalName, $data['file_type'], $data['file_size'] * 1024, $this->getPath($data['file_name'], $user), $user);
|
|
|
|
|
|
|
|
return '/f/' . $data['raw_name'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function uploadImage($name, $max_size, $originalName, $max_width, $user = null) {
|
|
|
|
$config['upload_path'] = '.' . DIRECTORY_SEPARATOR . 'files' . DIRECTORY_SEPARATOR;
|
|
|
|
if($user != null) {
|
|
|
|
$config['upload_path'] .= 'userContent' . DIRECTORY_SEPARATOR . $user . DIRECTORY_SEPARATOR;
|
|
|
|
}
|
2018-10-16 16:28:42 +00:00
|
|
|
$config['allowed_types'] = 'gif|jpg|png';
|
|
|
|
$config['max_size'] = $max_size;
|
2018-12-30 17:52:30 +00:00
|
|
|
$config['file_name'] = $this->generateName() . "." . pathinfo(basename($originalName), PATHINFO_EXTENSION);
|
2018-10-16 16:28:42 +00:00
|
|
|
|
|
|
|
$this->load->library('upload', $config);
|
|
|
|
|
|
|
|
if ( ! $this->upload->do_upload($name)) {
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
$data = $this->upload->data();
|
|
|
|
|
|
|
|
// Resize
|
|
|
|
$config['image_library'] = 'gd2';
|
|
|
|
$config['source_image'] = $data['full_path'];
|
|
|
|
$config['maintain_ratio'] = TRUE;
|
|
|
|
$config['width'] = $max_width;
|
|
|
|
|
|
|
|
$this->load->library('image_lib', $config);
|
|
|
|
|
|
|
|
$this->image_lib->resize();
|
|
|
|
|
2018-12-30 17:52:30 +00:00
|
|
|
$this->addToDatabase($data['raw_name'], $originalName, $data['file_type'], $data['file_size'] * 1024, $this->getPath($data['file_name'], $user), $user);
|
2018-12-26 17:19:28 +00:00
|
|
|
|
|
|
|
echo shell_exec('python /var/www/codeigniter/duplicates.py');
|
2018-11-01 15:09:22 +00:00
|
|
|
|
2018-10-27 10:08:54 +00:00
|
|
|
return '/f/' . $data['raw_name'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function uploadCroppedImage($name, $max_size, $originalname, $width, $height, $userContent = true) {
|
|
|
|
$config['upload_path'] = '.' . DIRECTORY_SEPARATOR . 'files' . DIRECTORY_SEPARATOR . ($userContent ? 'userContent' . DIRECTORY_SEPARATOR : '');
|
|
|
|
$config['allowed_types'] = 'gif|jpg|png';
|
|
|
|
$config['max_size'] = $max_size;
|
|
|
|
$config['file_name'] = $this->generateName() . "." . pathinfo(basename($originalname), PATHINFO_EXTENSION);
|
|
|
|
|
|
|
|
$this->load->library('upload', $config);
|
|
|
|
|
|
|
|
if ( ! $this->upload->do_upload($name)) {
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
$data = $this->upload->data();
|
|
|
|
|
|
|
|
// Resize
|
|
|
|
$config['image_library'] = 'gd2';
|
|
|
|
$config['source_image'] = $data['full_path'];
|
|
|
|
$config['maintain_ratio'] = TRUE;
|
|
|
|
|
|
|
|
$size = getimagesize($data['full_path']);
|
|
|
|
|
|
|
|
if($size[0] > $size[1]) {
|
|
|
|
$config['height'] = $height;
|
|
|
|
} else {
|
|
|
|
$config['width'] = $width;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->load->library('image_lib');
|
|
|
|
$this->image_lib->initialize($config);
|
|
|
|
$this->image_lib->resize();
|
|
|
|
|
|
|
|
$config['source_image'] = $config['upload_path'] . $config['file_name'];
|
|
|
|
$config['maintain_ratio'] = FALSE;
|
|
|
|
$config['height'] = $height;
|
|
|
|
$config['width'] = $width;
|
|
|
|
|
|
|
|
$size = getimagesize($config['source_image']);
|
|
|
|
|
|
|
|
if($size[0] > $size[1]) {
|
|
|
|
$config['x_axis'] = ($size[0] - $width) / 2;
|
|
|
|
} else {
|
|
|
|
$config['y_axis'] = ($size[1] - $height) / 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->image_lib->clear();
|
|
|
|
$this->image_lib->initialize($config);
|
|
|
|
$this->image_lib->crop();
|
|
|
|
|
|
|
|
$this->db->query('INSERT INTO files (name, original_name, type, size, path, isUserData) VALUES (?, ?, ?, ?, ?, ?)', [$data['raw_name'], $originalname, $data['file_type'], $data['file_size'] * 1024, $this->getPath($data['file_name'], $userContent), $userContent]);
|
2018-11-01 15:09:22 +00:00
|
|
|
|
2018-12-26 17:19:28 +00:00
|
|
|
$this->db->cache_delete('admin', 'files');
|
|
|
|
|
|
|
|
echo shell_exec('python /var/www/codeigniter/duplicates.py');
|
2018-11-01 15:09:22 +00:00
|
|
|
|
2018-10-16 16:28:42 +00:00
|
|
|
return '/f/' . $data['raw_name'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-27 10:08:54 +00:00
|
|
|
public function uploadFileByContent($content, $original_name, $fullType, $fileSize, $userContent = true) {
|
2018-12-26 17:19:28 +00:00
|
|
|
$target_dir = 'files' . DIRECTORY_SEPARATOR . ($userContent ? 'userContent' . DIRECTORY_SEPARATOR : '');
|
2018-10-16 16:28:42 +00:00
|
|
|
$filetype = pathinfo(basename($original_name), PATHINFO_EXTENSION);
|
2018-12-26 17:19:28 +00:00
|
|
|
$name = $this->generateName();
|
|
|
|
$target_file = $target_dir . $name . '.' . $filetype;
|
2018-10-16 16:28:42 +00:00
|
|
|
|
|
|
|
$fp = fopen($target_file, 'w');
|
|
|
|
fwrite($fp, $content);
|
|
|
|
fclose($fp);
|
|
|
|
|
2018-12-26 17:19:28 +00:00
|
|
|
$target_file = str_replace('\\', '/', $target_file);
|
|
|
|
|
2018-10-27 10:08:54 +00:00
|
|
|
$this->db->query('INSERT INTO files (name, original_name, type, size, path, isUserData) VALUES (?, ?, ?, ?, ?, ?)', [$name, $original_name, $fullType, $fileSize, $target_file, $userContent]);
|
2018-10-16 16:28:42 +00:00
|
|
|
|
2018-12-26 17:19:28 +00:00
|
|
|
$this->db->cache_delete('admin', 'files');
|
|
|
|
|
|
|
|
echo shell_exec('python /var/www/codeigniter/duplicates.py');
|
2018-11-01 15:09:22 +00:00
|
|
|
|
2018-10-16 16:28:42 +00:00
|
|
|
return '/f/' . $name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function generateName()
|
|
|
|
{
|
|
|
|
return md5(uniqid(rand(), true));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getFileList()
|
|
|
|
{
|
|
|
|
$unfiltered_result = $this->db->query('SELECT * FROM files')->result_array();
|
|
|
|
$result = [];
|
|
|
|
foreach ($unfiltered_result as $item) {
|
|
|
|
$item['size'] = NumberHumanizer::binarySuffix(intval($item['size']), $_SESSION['site_lang']);
|
|
|
|
$result[] = $item;
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2018-12-30 17:52:30 +00:00
|
|
|
public function getFileID($name, $userID = null) {
|
|
|
|
$result = $this->db->query('SELECT ID FROM files WHERE name = ? AND user = ?', [$name, $userID])->result_array();
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getFilePath($name, $userID = null) {
|
|
|
|
$result = $this->db->query('SELECT path FROM files WHERE name = ? AND user = ?', [$name, $userID])->result_array();
|
|
|
|
return !empty($result) ? $result[0]['path'] : null;
|
|
|
|
}
|
|
|
|
|
2018-10-16 16:28:42 +00:00
|
|
|
public function delete($id) {
|
|
|
|
$filePath = $this->db->query('SELECT path FROM files WHERE ID = ? LIMIT 1', [$id])->result_array()[0];
|
|
|
|
unlink($filePath['path']);
|
|
|
|
|
|
|
|
$this->db->query('DELETE FROM files WHERE ID = ? LIMIT 1', [$id]);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|