Import module now without static functions

This commit is contained in:
Tobias Reich 2015-06-29 09:45:03 +02:00
parent 897fad76cf
commit f1ff8bf2b0
2 changed files with 66 additions and 33 deletions

View File

@ -219,14 +219,16 @@ class Admin extends Access {
private function importUrl() { private function importUrl() {
Module::dependencies(isset($_POST['url'], $_POST['albumID'])); Module::dependencies(isset($_POST['url'], $_POST['albumID']));
echo Import::url($_POST['url'], $_POST['albumID']); $import = new Import($this->database, $this->plugins, $this->settings);
echo $import->url($_POST['url'], $_POST['albumID']);
} }
private function importServer() { private function importServer() {
Module::dependencies(isset($_POST['albumID'], $_POST['path'])); Module::dependencies(isset($_POST['albumID'], $_POST['path']));
echo Import::server($_POST['albumID'], $_POST['path']); $import = new Import($this->database, $this->plugins, $this->settings);
echo $import->server($_POST['path'], $_POST['albumID']);
} }

View File

@ -9,14 +9,32 @@ if (!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
class Import extends Module { class Import extends Module {
static function photo($database, $plugins, $settings, $path, $albumID = 0, $description = '', $tags = '') { private $database = null;
private $settings = null;
private $albumIDs = null;
public function __construct($database, $plugins, $settings) {
# Init vars
$this->database = $database;
$this->plugins = $plugins;
$this->settings = $settings;
return true;
}
private function photo($path, $albumID = 0, $description = '', $tags = '') {
# Check dependencies
self::dependencies(isset($this->database, $this->plugins, $this->settings, $path));
# No need to validate photo type and extension in this function. # No need to validate photo type and extension in this function.
# $photo->add will take care of it. # $photo->add will take care of it.
$info = getimagesize($path); $info = getimagesize($path);
$size = filesize($path); $size = filesize($path);
$photo = new Photo($database, $plugins, $settings, null); $photo = new Photo($this->database, $this->plugins, $this->settings, null);
$nameFile = array(array()); $nameFile = array(array());
$nameFile[0]['name'] = $path; $nameFile[0]['name'] = $path;
@ -25,30 +43,35 @@ class Import extends Module {
$nameFile[0]['error'] = 0; $nameFile[0]['error'] = 0;
$nameFile[0]['size'] = $size; $nameFile[0]['size'] = $size;
if (!$photo->add($nameFile, $albumID, $description, $tags)) return false; if (!$photo->add($nameFile, $albumID, $description, $tags, true)) return false;
return true; return true;
} }
static function url($urls, $albumID = 0) { public function url($urls, $albumID = 0) {
global $database, $plugins, $settings; # Check dependencies
self::dependencies(isset($this->database, $urls));
# Call plugins
$this->plugins(__METHOD__, 0, func_get_args());
$error = false; $error = false;
# Parse # Parse URLs
$urls = str_replace(' ', '%20', $urls); $urls = str_replace(' ', '%20', $urls);
$urls = explode(',', $urls); $urls = explode(',', $urls);
foreach ($urls as &$url) { foreach ($urls as &$url) {
# Validate photo type and extension even when Import::photo (=> $photo->add) will do the same. # Validate photo type and extension even when $this->photo (=> $photo->add) will do the same.
# This prevents us from downloading invalid photos. # This prevents us from downloading invalid photos.
# Verify extension # Verify extension
$extension = getExtension($url); $extension = getExtension($url);
if (!in_array(strtolower($extension), Photo::$validExtensions, true)) { if (!in_array(strtolower($extension), Photo::$validExtensions, true)) {
$error = true; $error = true;
Log::error($this->database, __METHOD__, __LINE__, 'Photo format not supported (' . $url . ')');
continue; continue;
} }
@ -56,6 +79,7 @@ class Import extends Module {
$type = @exif_imagetype($url); $type = @exif_imagetype($url);
if (!in_array($type, Photo::$validTypes, true)) { if (!in_array($type, Photo::$validTypes, true)) {
$error = true; $error = true;
Log::error($this->database, __METHOD__, __LINE__, 'Photo type not supported (' . $url . ')');
continue; continue;
} }
@ -65,34 +89,38 @@ class Import extends Module {
if (@copy($url, $tmp_name)===false) { if (@copy($url, $tmp_name)===false) {
$error = true; $error = true;
Log::error($database, __METHOD__, __LINE__, 'Could not copy file (' . $tmp_name . ') to temp-folder (' . $tmp_name . ')'); Log::error($this->database, __METHOD__, __LINE__, 'Could not copy file (' . $tmp_name . ') to temp-folder (' . $tmp_name . ')');
continue; continue;
} }
# Import photo # Import photo
if (!Import::photo($database, $plugins, $settings, $tmp_name, $albumID)) { if (!$this->photo($tmp_name, $albumID)) {
$error = true; $error = true;
Log::error($database, __METHOD__, __LINE__, 'Could not import file: ' . $tmp_name); Log::error($this->database, __METHOD__, __LINE__, 'Could not import file: ' . $tmp_name);
continue; continue;
} }
} }
# Call plugins
$this->plugins(__METHOD__, 1, func_get_args());
if ($error===false) return true; if ($error===false) return true;
return false; return false;
} }
static function server($albumID = 0, $path) { public function server($path, $albumID = 0) {
global $database, $plugins, $settings; # Check dependencies
self::dependencies(isset($this->database, $this->plugins, $this->settings));
# Parse path # Parse path
if (!isset($path)) $path = LYCHEE_UPLOADS_IMPORT; if (!isset($path)) $path = LYCHEE_UPLOADS_IMPORT;
if (substr($path, -1)==='/') $path = substr($path, 0, -1); if (substr($path, -1)==='/') $path = substr($path, 0, -1);
if (is_dir($path)===false) { if (is_dir($path)===false) {
Log::error($database, __METHOD__, __LINE__, 'Given path is not a directory (' . $path . ')'); Log::error($this->database, __METHOD__, __LINE__, 'Given path is not a directory (' . $path . ')');
return 'Error: Given path is not a directory!'; return 'Error: Given path is not a directory!';
} }
@ -100,7 +128,7 @@ class Import extends Module {
if ($path===LYCHEE_UPLOADS_BIG||($path . '/')===LYCHEE_UPLOADS_BIG|| if ($path===LYCHEE_UPLOADS_BIG||($path . '/')===LYCHEE_UPLOADS_BIG||
$path===LYCHEE_UPLOADS_MEDIUM||($path . '/')===LYCHEE_UPLOADS_MEDIUM|| $path===LYCHEE_UPLOADS_MEDIUM||($path . '/')===LYCHEE_UPLOADS_MEDIUM||
$path===LYCHEE_UPLOADS_THUMB||($path . '/')===LYCHEE_UPLOADS_THUMB) { $path===LYCHEE_UPLOADS_THUMB||($path . '/')===LYCHEE_UPLOADS_THUMB) {
Log::error($database, __METHOD__, __LINE__, 'The given path is a reserved path of Lychee (' . $path . ')'); Log::error($this->database, __METHOD__, __LINE__, 'The given path is a reserved path of Lychee (' . $path . ')');
return 'Error: Given path is a reserved path of Lychee!'; return 'Error: Given path is a reserved path of Lychee!';
} }
@ -108,10 +136,10 @@ class Import extends Module {
$contains['photos'] = false; $contains['photos'] = false;
$contains['albums'] = false; $contains['albums'] = false;
# Invoke plugins directly, as instance method not valid here # Call plugins
# Note that updated albumId and path explicitly passed, rather # Note that updated albumId and path explicitly passed, rather
# than using func_get_args() which will only return original ones # than using func_get_args() which will only return original ones
$plugins->activate(__METHOD__ . ":before", array($albumID, $path)); $this->plugins(__METHOD__, 0, array($albumID, $path));
# Get all files # Get all files
$files = glob($path . '/*'); $files = glob($path . '/*');
@ -122,7 +150,7 @@ class Import extends Module {
# the file may still be unreadable by the user # the file may still be unreadable by the user
if (!is_readable($file)) { if (!is_readable($file)) {
$error = true; $error = true;
Log::error($database, __METHOD__, __LINE__, 'Could not read file or directory: ' . $file); Log::error($this->database, __METHOD__, __LINE__, 'Could not read file or directory: ' . $file);
continue; continue;
} }
@ -130,33 +158,33 @@ class Import extends Module {
# Photo # Photo
if (!Import::photo($database, $plugins, $settings, $file, $albumID)) { $contains['photos'] = true;
if (!$this->photo($file, $albumID)) {
$error = true; $error = true;
Log::error($database, __METHOD__, __LINE__, 'Could not import file: ' . $file); Log::error($this->database, __METHOD__, __LINE__, 'Could not import file: ' . $file);
continue; continue;
} }
$contains['photos'] = true;
} else if (is_dir($file)) { } else if (is_dir($file)) {
# Folder # Folder
$name = mysqli_real_escape_string($database, basename($file)); $album = new Album($this->database, $this->plugins, $this->settings, null);
$album = new Album($database, null, null, null); $newAlbumID = $album->add('[Import] ' . basename($file));
$newAlbumID = $album->add('[Import] ' . $name);
$contains['albums'] = true; $contains['albums'] = true;
if ($newAlbumID===false) { if ($newAlbumID===false) {
$error = true; $error = true;
Log::error($database, __METHOD__, __LINE__, 'Could not create album in Lychee (' . $newAlbumID . ')'); Log::error($this->database, __METHOD__, __LINE__, 'Could not create album in Lychee (' . $newAlbumID . ')');
continue; continue;
} }
$import = Import::server($newAlbumID, $file . '/'); $import = $this->server($file . '/', $newAlbumID);
if ($import!==true&&$import!=='Notice: Import only contains albums!') { if ($import!==true&&$import!=='Notice: Import only contains albums!') {
$error = true; $error = true;
Log::error($database, __METHOD__, __LINE__, 'Could not import folder. Function returned warning'); Log::error($this->database, __METHOD__, __LINE__, 'Could not import folder. Function returned warning.');
continue; continue;
} }
@ -164,13 +192,16 @@ class Import extends Module {
} }
# Invoke plugins directly, as instance method not valid here # Call plugins
# Note that updated albumId and path explicitly passed, rather # Note that updated albumId and path explicitly passed, rather
# than using func_get_args() which will only return original ones # than using func_get_args() which will only return original ones
$plugins->activate(__METHOD__ . ":after", array($albumID, $path)); $this->plugins(__METHOD__, 1, array($albumID, $path));
# The following returns will be caught in the front-end
if ($contains['photos']===false&&$contains['albums']===false) return 'Warning: Folder empty or no readable files to process!'; if ($contains['photos']===false&&$contains['albums']===false) return 'Warning: Folder empty or no readable files to process!';
if ($contains['photos']===false&&$contains['albums']===true) return 'Notice: Import only contains albums!'; if ($contains['photos']===false&&$contains['albums']===true) return 'Notice: Import only contained albums!';
if ($error===true) return false;
return true; return true;
} }