This repository has been archived on 2024-06-26. You can view files and clone it, but cannot push or open issues or pull requests.
sakura/app/File.php

137 lines
3.1 KiB
PHP
Raw Normal View History

<?php
2016-02-03 22:22:56 +00:00
/**
* Holds the file server.
2016-03-08 23:07:58 +00:00
*
2016-02-03 22:22:56 +00:00
* @package Sakura
*/
namespace Sakura;
2016-01-17 01:58:31 +00:00
use finfo;
/**
2016-02-02 21:04:15 +00:00
* Used for storing files served through Sakura.
2016-03-08 23:07:58 +00:00
*
* @package Sakura
2016-02-02 21:04:15 +00:00
* @author Julian van de Groep <me@flash.moe>
*/
class File
{
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.
2016-03-08 23:07:58 +00:00
*
2016-02-02 21:04:15 +00:00
* @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.
2016-03-08 23:07:58 +00:00
*
2016-02-02 21:04:15 +00:00
* @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.
2016-03-08 23:07:58 +00:00
*
2016-02-02 21:04:15 +00:00
* @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.
2016-03-08 23:07:58 +00:00
*
2016-02-02 21:04:15 +00:00
* @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.
2016-03-08 23:07:58 +00:00
*
2016-02-02 21:04:15 +00:00
* @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).
2016-03-08 23:07:58 +00:00
*
2016-02-02 21:04:15 +00:00
* @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.
2016-03-08 23:07:58 +00:00
*
2016-02-02 21:04:15 +00:00
* @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.
2016-03-08 23:07:58 +00:00
*
2016-02-02 21:04:15 +00:00
* @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
2016-02-25 16:06:29 +00: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 01:58:31 +00:00
2016-08-04 21:24:08 +00:00
// Save the file data
file_put_contents(config('file.uploads_dir') . $id . ".bin", $data);
2016-01-17 01:58:31 +00:00
// Return a new File object
return new File($id);
}
2016-02-02 21:04:15 +00:00
/**
* Constructor.
2016-03-08 23:07:58 +00:00
*
2016-02-02 21:04:15 +00:00
* @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
2016-02-25 16:06:29 +00:00
$fileRow = DB::table('uploads')
->where('file_id', $fileId)
->get();
2016-01-17 01:58:31 +00:00
// If anything was returned populate the variables
if ($fileRow) {
2016-02-25 16:06:29 +00:00
$fileRow = $fileRow[0];
2016-02-18 23:28:44 +00:00
$this->id = $fileRow->file_id;
$this->user = User::construct($fileRow->user_id);
2016-08-04 21:24:08 +00:00
$this->data = file_get_contents(config('file.uploads_dir') . $fileRow->file_id . ".bin");
2016-02-18 23:28:44 +00:00
$this->name = $fileRow->file_name;
$this->mime = $fileRow->file_mime;
$this->time = $fileRow->file_time;
$this->expire = $fileRow->file_expire;
2016-01-17 01:58:31 +00:00
}
}
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()
{
2016-08-04 21:24:08 +00:00
unlink(config('file.uploads_dir') . $this->id . ".bin");
2016-02-25 16:06:29 +00:00
DB::table('uploads')
->where('file_id', $this->id)
->delete();
2016-01-17 01:58:31 +00:00
}
}