2015-09-15 22:37:29 +02:00
< ? php
2016-02-03 23:22:56 +01:00
/**
* Holds the file server .
* @ package Sakura
*/
2015-09-15 22:37:29 +02:00
namespace Sakura ;
2016-01-17 02:58:31 +01:00
use finfo ;
2015-10-18 21:06:30 +02:00
/**
2016-02-02 22:04:15 +01:00
* Used for storing files served through Sakura .
2015-10-18 21:06:30 +02:00
* @ package Sakura
2016-02-02 22:04:15 +01:00
* @ author Julian van de Groep < me @ flash . moe >
2015-10-18 21:06:30 +02:00
*/
2015-11-06 23:30:37 +01:00
class File
2015-09-15 22:37:29 +02:00
{
2016-02-02 22:04:15 +01:00
/**
* ID of the file .
* @ var int
*/
2016-01-17 02:58:31 +01:00
public $id = 0 ;
2016-02-02 22:04:15 +01:00
/**
* User instance of the user that uploaded this file .
* @ var User
*/
2016-01-17 02:58:31 +01:00
public $user = null ;
2016-02-02 22:04:15 +01:00
/**
* Data of the file .
* @ var string
*/
2016-01-17 02:58:31 +01:00
public $data = null ;
2016-02-02 22:04:15 +01:00
/**
* Original filename of the file .
* @ var string
*/
2016-01-17 02:58:31 +01:00
public $name = null ;
2016-02-02 22:04:15 +01:00
/**
* Mime type of the file .
* @ var string
*/
2016-01-17 02:58:31 +01:00
public $mime = null ;
2016-02-02 22:04:15 +01:00
/**
2016-08-05 04:35:37 +02:00
* The timestamp of when this file was created .
2016-02-02 22:04:15 +01:00
* @ var int
*/
2016-01-17 02:58:31 +01:00
public $time = 0 ;
2016-02-02 22:04:15 +01:00
/**
* The UNIX timestamp of when this file should automatically remove itself ( currently unused ) .
* @ var int
*/
2016-01-17 02:58:31 +01:00
public $expire = 0 ;
2016-02-02 22:04:15 +01:00
/**
* Create a new file .
2016-08-05 04:35:37 +02:00
* @ param string $data
* @ param string $name
* @ param User $user
* @ param int $expire
* @ return File
2016-02-02 22:04:15 +01:00
*/
2016-12-04 17:33:52 +01:00
public static function create ( string $data , string $name , User $user , int $expire = 0 ) : File
2016-01-17 02:58:31 +01:00
{
// Get the mimetype
$mime = ( new finfo ( FILEINFO_MIME_TYPE )) -> buffer ( $data );
// Insert it into the database
2016-02-25 17:06:29 +01:00
$id = DB :: table ( 'uploads' )
-> insertGetId ([
'user_id' => $user -> id ,
'file_name' => $name ,
'file_mime' => $mime ,
'file_time' => time (),
'file_expire' => $expire ,
]);
2016-01-17 02:58:31 +01:00
2016-08-04 23:24:08 +02:00
// Save the file data
2016-10-07 19:37:00 +02:00
file_put_contents ( path ( config ( 'file.uploads_dir' ) . $id . " .bin " ), $data );
2016-08-04 23:24:08 +02:00
2016-01-17 02:58:31 +01:00
// Return a new File object
return new File ( $id );
}
2016-02-02 22:04:15 +01:00
/**
* Constructor .
2016-08-05 04:35:37 +02:00
* @ param int $fileId
2016-02-02 22:04:15 +01:00
*/
2016-12-04 17:33:52 +01:00
public function __construct ( int $fileId )
2016-01-17 02:58:31 +01:00
{
// Attempt to get the database row
2016-02-25 17:06:29 +01:00
$fileRow = DB :: table ( 'uploads' )
-> where ( 'file_id' , $fileId )
2016-08-06 16:09:01 +02:00
-> first ();
2016-01-17 02:58:31 +01:00
// If anything was returned populate the variables
if ( $fileRow ) {
2016-02-19 00:28:44 +01:00
$this -> id = $fileRow -> file_id ;
$this -> user = User :: construct ( $fileRow -> user_id );
2016-10-07 19:37:00 +02:00
$this -> data = file_get_contents ( path ( config ( 'file.uploads_dir' ) . $fileRow -> file_id . " .bin " ));
2016-02-19 00:28:44 +01:00
$this -> name = $fileRow -> file_name ;
$this -> mime = $fileRow -> file_mime ;
$this -> time = $fileRow -> file_time ;
$this -> expire = $fileRow -> file_expire ;
2016-01-17 02:58:31 +01:00
}
}
2016-02-02 22:04:15 +01:00
/**
* Delete this file from the database .
*/
2016-12-04 17:33:52 +01:00
public function delete () : void
2016-01-17 02:58:31 +01:00
{
2016-10-07 19:37:00 +02:00
$filename = path ( config ( 'file.uploads_dir' ) . $this -> id . " .bin " );
2016-08-06 16:09:01 +02:00
if ( file_exists ( $filename )) {
unlink ( $filename );
}
2016-08-04 23:24:08 +02:00
2016-02-25 17:06:29 +01:00
DB :: table ( 'uploads' )
-> where ( 'file_id' , $this -> id )
-> delete ();
2016-01-17 02:58:31 +01:00
}
2015-09-15 22:37:29 +02:00
}