Initial commit
This commit is contained in:
181
app/plugins/fileshare/models/upload.php
Normal file
181
app/plugins/fileshare/models/upload.php
Normal file
@ -0,0 +1,181 @@
|
||||
<?php
|
||||
/**
|
||||
* app/plugins/fileshare/models/upload.php
|
||||
*
|
||||
* This class is used for the manipulation of the uploads database table.
|
||||
*
|
||||
* @package TP FileShare
|
||||
* @version 3.0
|
||||
* @author Joey Kimsey <Joey@thetempusproject.com>
|
||||
* @link https://TheTempusProject.com
|
||||
* @license https://opensource.org/licenses/MIT [MIT LICENSE]
|
||||
*/
|
||||
namespace TheTempusProject\Models;
|
||||
|
||||
use TheTempusProject\Bedrock\Classes\Config;
|
||||
use TheTempusProject\Bedrock\Functions\Check;
|
||||
use TheTempusProject\Canary\Canary as Debug;
|
||||
use TheTempusProject\Classes\DatabaseModel;
|
||||
use TheTempusProject\TheTempusProject as App;
|
||||
use TheTempusProject\Bedrock\Classes\CustomException;
|
||||
|
||||
class Upload extends DatabaseModel {
|
||||
public $tableName = 'uploads';
|
||||
public $databaseMatrix = [
|
||||
[ 'name', 'varchar', '256' ],
|
||||
[ 'location', 'text', '64' ],
|
||||
[ 'size', 'int', '20' ],
|
||||
[ 'file_type', 'varchar', '64' ],
|
||||
[ 'createdBy', 'int', '11' ],
|
||||
[ 'createdAt', 'int', '11' ],
|
||||
];
|
||||
|
||||
/**
|
||||
* The model constructor.
|
||||
*/
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public function create( $name, $location ) {
|
||||
$info = $this->getFileInfo( $location );
|
||||
$size = $info['size'];
|
||||
$file_type = $info['file_type'];
|
||||
$fields = [
|
||||
'name' => $name,
|
||||
'location' => '/' . $location,
|
||||
'size' => $size,
|
||||
'file_type' => $file_type,
|
||||
'createdAt' => time(),
|
||||
'createdBy' => App::$activeUser->ID,
|
||||
];
|
||||
if ( ! self::$db->insert( $this->tableName, $fields ) ) {
|
||||
Debug::info( 'Uploads::create - failed to insert to db' );
|
||||
return false;
|
||||
}
|
||||
return self::$db->lastId();
|
||||
}
|
||||
|
||||
public function update( $id, $name, $location = '' ) {
|
||||
$fields = [
|
||||
'name' => $name,
|
||||
];
|
||||
if ( ! empty( $location ) ) {
|
||||
$info = $this->getFileInfo( $location );
|
||||
$fields['location'] = $location;
|
||||
$fields['size'] = $info['size'];
|
||||
$fields['file_type'] = $info['file_type'];
|
||||
}
|
||||
if ( ! self::$db->update( $this->tableName, $id, $fields ) ) {
|
||||
new CustomException( 'UploadsUpdate' );
|
||||
Debug::error( "Uploads: $id not updated: $fields" );
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public function simple() {
|
||||
$uploads = self::$db->get( $this->tableName, '*' );
|
||||
if ( !$uploads->count() ) {
|
||||
Debug::warn( 'Could not find any uploads' );
|
||||
return false;
|
||||
}
|
||||
|
||||
$uploads = $uploads->results();
|
||||
$out = [];
|
||||
foreach ( $uploads as $upload ) {
|
||||
$out[ $upload->name ] = $upload->location;
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
// public function processFileUpload() {
|
||||
|
||||
// }
|
||||
|
||||
public function getByUser( $limit = 0 ) {
|
||||
$whereClause = [
|
||||
'createdBy', '=', App::$activeUser->ID,
|
||||
];
|
||||
if ( empty( $limit ) ) {
|
||||
$uploads = self::$db->get( $this->tableName, $whereClause );
|
||||
} else {
|
||||
$uploads = self::$db->get( $this->tableName, $whereClause, 'ID', 'DESC', [0, $limit] );
|
||||
}
|
||||
if ( ! $uploads->count() ) {
|
||||
Debug::info( 'No Uploads found.' );
|
||||
return false;
|
||||
}
|
||||
return $this->filter( $uploads->results() );
|
||||
}
|
||||
|
||||
public function delete( $idArray ) {
|
||||
if ( !is_array( $idArray ) ) {
|
||||
$idArray = [ $idArray ];
|
||||
}
|
||||
|
||||
foreach ( $idArray as $id ) {
|
||||
$upload = self::findById( $id );
|
||||
if ( $upload ) {
|
||||
$fileLocation = APP_ROOT_DIRECTORY . ltrim( $upload->location, '/' );
|
||||
if (file_exists($fileLocation)) {
|
||||
if (!unlink($fileLocation)) {
|
||||
Debug::error("Failed to delete file: $fileLocation");
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
Debug::warn("File does not exist: $fileLocation");
|
||||
}
|
||||
}
|
||||
}
|
||||
return parent::delete( $idArray );
|
||||
}
|
||||
|
||||
private function getFileInfo( $imagePath )
|
||||
{
|
||||
$data = [];
|
||||
$fileLocation = APP_ROOT_DIRECTORY . ltrim( $imagePath, '/' );
|
||||
if ( file_exists( $fileLocation ) ) {
|
||||
$fileInfo = finfo_open(FILEINFO_MIME_TYPE);
|
||||
$mimeType = finfo_file($fileInfo, $fileLocation);
|
||||
finfo_close($fileInfo);
|
||||
|
||||
$fileSize = filesize($fileLocation);
|
||||
|
||||
$data['file_type'] = $mimeType;
|
||||
$data['size'] = $fileSize;
|
||||
$data['path'] = $fileLocation;
|
||||
} else {
|
||||
Debug::warn("File does not exist: $fileLocation");
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function filter( $data, $params = [] ) {
|
||||
foreach ( $data as $instance ) {
|
||||
if ( !is_object( $instance ) ) {
|
||||
$instance = $data;
|
||||
$end = true;
|
||||
}
|
||||
if (isset($instance->size)) {
|
||||
$instance->readableSize = $this->formatSize($instance->size);
|
||||
}
|
||||
$out[] = $instance;
|
||||
if ( !empty( $end ) ) {
|
||||
$out = $out[0];
|
||||
break;
|
||||
}
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
public function formatSize($size) {
|
||||
$units = ['B', 'KB', 'MB', 'GB', 'TB'];
|
||||
$i = 0;
|
||||
while ($size >= 1024 && $i < count($units) - 1) {
|
||||
$size /= 1024;
|
||||
$i++;
|
||||
}
|
||||
return round($size, 2) . ' ' . $units[$i];
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user