2015-09-15 20:37:29 +00:00
|
|
|
<?php
|
|
|
|
namespace Sakura;
|
|
|
|
|
2016-01-17 01:58:31 +00:00
|
|
|
use finfo;
|
|
|
|
|
2015-10-18 19:06:30 +00:00
|
|
|
/**
|
2016-02-02 21:04:15 +00:00
|
|
|
* Used for storing files served through Sakura.
|
|
|
|
*
|
2015-10-18 19:06:30 +00:00
|
|
|
* @package Sakura
|
2016-02-02 21:04:15 +00:00
|
|
|
* @author Julian van de Groep <me@flash.moe>
|
2015-10-18 19:06:30 +00:00
|
|
|
*/
|
2015-11-06 22:30:37 +00:00
|
|
|
class File
|
2015-09-15 20:37:29 +00:00
|
|
|
{
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* ID of the file.
|
|
|
|
* @var int
|
|
|
|
*/
|
2016-01-17 01:58:31 +00:00
|
|
|
public $id = 0;
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* User instance of the user that uploaded this file.
|
|
|
|
*
|
|
|
|
* @var User
|
|
|
|
*/
|
2016-01-17 01:58:31 +00:00
|
|
|
public $user = null;
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Data of the file.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-01-17 01:58:31 +00:00
|
|
|
public $data = null;
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Original filename of the file.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-01-17 01:58:31 +00:00
|
|
|
public $name = null;
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Mime type of the file.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-01-17 01:58:31 +00:00
|
|
|
public $mime = null;
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The UNIX timestamp of when this file was created.
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
2016-01-17 01:58:31 +00:00
|
|
|
public $time = 0;
|
2016-02-02 21:04:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The UNIX timestamp of when this file should automatically remove itself (currently unused).
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
2016-01-17 01:58:31 +00:00
|
|
|
public $expire = 0;
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Create a new file.
|
|
|
|
*
|
|
|
|
* @param string $data Contents of the file.
|
|
|
|
* @param string $name Name of the file.
|
|
|
|
* @param User $user User instance of the user creating this file.
|
|
|
|
* @param int $expire UNIX timestamp of when this file should automatically remove itself.
|
|
|
|
*
|
|
|
|
* @return File The created file instance for the file.
|
|
|
|
*/
|
2016-01-17 01:58:31 +00:00
|
|
|
public static function create($data, $name, User $user, $expire = 0)
|
|
|
|
{
|
|
|
|
// Get the mimetype
|
|
|
|
$mime = (new finfo(FILEINFO_MIME_TYPE))->buffer($data);
|
|
|
|
|
|
|
|
// Insert it into the database
|
|
|
|
Database::insert('uploads', [
|
|
|
|
'user_id' => $user->id,
|
|
|
|
'file_data' => $data,
|
|
|
|
'file_name' => $name,
|
|
|
|
'file_mime' => $mime,
|
|
|
|
'file_time' => time(),
|
|
|
|
'file_expire' => $expire,
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Get the last insert id
|
|
|
|
$id = Database::lastInsertID();
|
|
|
|
|
|
|
|
// Return a new File object
|
|
|
|
return new File($id);
|
|
|
|
}
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* @param int $fileId ID of the file that should be constructed.
|
|
|
|
*/
|
2016-01-17 01:58:31 +00:00
|
|
|
public function __construct($fileId)
|
|
|
|
{
|
|
|
|
// Attempt to get the database row
|
|
|
|
$fileRow = Database::fetch('uploads', false, ['file_id' => [$fileId, '=']]);
|
|
|
|
|
|
|
|
// If anything was returned populate the variables
|
|
|
|
if ($fileRow) {
|
|
|
|
$this->id = $fileRow['file_id'];
|
|
|
|
$this->user = User::construct($fileRow['user_id']);
|
|
|
|
$this->data = $fileRow['file_data'];
|
|
|
|
$this->name = $fileRow['file_name'];
|
|
|
|
$this->mime = $fileRow['file_mime'];
|
|
|
|
$this->time = $fileRow['file_time'];
|
|
|
|
$this->expire = $fileRow['file_expire'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-02 21:04:15 +00:00
|
|
|
/**
|
|
|
|
* Delete this file from the database.
|
|
|
|
*/
|
2016-01-17 01:58:31 +00:00
|
|
|
public function delete()
|
|
|
|
{
|
|
|
|
Database::delete('uploads', [
|
|
|
|
'file_id' => [$this->id, '='],
|
|
|
|
]);
|
|
|
|
}
|
2015-09-15 20:37:29 +00:00
|
|
|
}
|