Select Git revision
VipsFile.php
Forked from
Uni Osnabrück / Plugins / Vips
Source project has a limited visibility.
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
VipsFile.php 4.68 KiB
<?php
/*
* VipsFile.php - Vips test class for Stud.IP
* Copyright (c) 2014 Elmar Ludwig
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*/
class VipsFile extends SimpleORMap
{
/**
* Configure the database mapping.
*/
protected static function configure($config = [])
{
$config['db_table'] = 'vips_file';
$config['has_and_belongs_to_many']['exercises'] = [
'class_name' => 'Exercise',
'assoc_foreign_key' => 'id',
'thru_table' => 'vips_file_ref',
'thru_key' => 'file_id',
'thru_assoc_key' => 'object_id',
'order_by' => "AND vips_file_ref.type = 'exercise'"
];
$config['has_and_belongs_to_many']['solutions'] = [
'class_name' => 'VipsSolution',
'thru_table' => 'vips_file_ref',
'thru_key' => 'file_id',
'thru_assoc_key' => 'object_id',
'order_by' => "AND vips_file_ref.type = 'solution'"
];
$config['belongs_to']['user'] = [
'class_name' => 'User',
'foreign_key' => 'user_id'
];
parent::configure($config);
}
/**
* Create a new VipsFile instance for this upload.
*/
public static function createWithFile($name, $path, $user_id)
{
$file = VipsFile::create([
'user_id' => $user_id,
'mime_type' => get_mime_type($name),
'name' => basename($name),
'size' => filesize($path),
'created' => date('Y-m-d H:i:s')
]);
if (is_uploaded_file($path)) {
$success = move_uploaded_file($path, $file->getFilePath(true));
} else {
$success = copy($path, $file->getFilePath(true));
}
if (!$success) {
$file->delete();
$file = null;
}
return $file;
}
/**
* Create a new VipsFile instance from this string.
*/
public static function createWithString($name, $string, $user_id)
{
$file = VipsFile::create([
'user_id' => $user_id,
'mime_type' => get_mime_type($name),
'name' => basename($name),
'size' => strlen($string),
'created' => date('Y-m-d H:i:s')
]);
file_put_contents($file->getFilePath(true), $string);
return $file;
}
/**
* Wrap an existing Stud.IP File object into a VipsFile instance.
*/
public static function wrapStudipFile($file)
{
return VipsFile::build([
'id' => $file->id,
'user_id' => $file->user_id,
'mime_type' => $file->mime_type,
'name' => $file->name,
'size' => $file->size,
'created' => date('Y-m-d H:i:s', $file->mkdate)
]);
}
/**
* Get the (context independent) download URL of this file.
*/
public function getDownloadURL()
{
$old = URLHelper::setBaseUrl($GLOBALS['ABSOLUTE_URI_STUDIP']);
$url = URLHelper::getScriptURL('sendfile.php', ['file_id' => $this->id]);
URLHelper::setBaseUrl($old);
return $url;
}
/**
* Return the absolute path of an uploaded file. The uploaded files
* are organized in sub-folders of UPLOAD_PATH to avoid performance
* problems with large directories.
*/
public function getFilePath($create = false)
{
if ($this->id === null) {
return null;
}
$dir = $GLOBALS['UPLOAD_PATH'] . '/' . substr($this->id, 0, 2);
if ($create && !file_exists($dir)) {
mkdir($dir);
}
return $dir . '/' . $this->id;
}
/**
* Delete this file instance and the associated upload.
*/
public function delete()
{
$path = $this->getFilePath();
if (file_exists($path)) {
unlink($path);
}
return parent::delete();
}
/**
* Determine if this file references an image file.
*/
public function isImage()
{
return strpos($this->mime_type, 'image/') === 0;
}
/**
* Determine if this file references an audio file.
*/
public function isAudio()
{
return strpos($this->mime_type, 'audio/') === 0;
}
/**
* Determine if this file references a video file.
*/
public function isVideo()
{
return strpos($this->mime_type, 'video/') === 0;
}
}