public function add($files, $albumID, $description = '', $tags = '') { # Check dependencies self::dependencies(isset($this->database)); # Check permissions if (hasPermissions(LYCHEE_UPLOADS) === false || hasPermissions(LYCHEE_UPLOADS_BIG) === false || hasPermissions(LYCHEE_UPLOADS_THUMB) === false) { Log::error($this->database, __METHOD__, __LINE__, 'An upload-folder is missing or not readable and writable'); exit('Error: An upload-folder is missing or not readable and writable!'); } # Call plugins $this->plugins(__METHOD__, 0, func_get_args()); switch ($albumID) { case 's': # s for public (share) $public = 1; $star = 0; $albumID = 0; break; case 'f': # f for starred (fav) $star = 1; $public = 0; $albumID = 0; break; case 'r': # r for recent $public = 0; $star = 0; $albumID = 0; break; default: $star = 0; $public = 0; break; } foreach ($files as $file) { # Verify file and set extension $mime_type = Video::getMimeType($file['tmp_name']); if ($mime_type === false) { Log::error($this->database, __METHOD__, __LINE__, 'Video format not supported'); exit('Error: Video format not supported!'); } $extension = self::$allowedMimeTypes[$mime_type]; # Generate id $id = str_replace('.', '', microtime(true)); while (strlen($id) < 14) { $id .= 0; } # Set paths $tmp_name = $file['tmp_name']; $video_name = md5($id) . $extension; $path = LYCHEE_UPLOADS_VIDEO . $video_name; $path_thumb = ""; # Create checksum based on $mime_type and $tmp_name $checksum = sha1_file($tmp_name); if ($checksum === false) { Log::error($this->database, __METHOD__, __LINE__, 'Could not calculate checksum for video'); exit('Error: Could not calculate checksum for video!'); } $exists = $this->exists($checksum); if ($exists !== false) { $video_name = $exists['video_name']; $path = $exists['path']; $path_thumb = $exists['path_thumb']; $exists = true; } if ($exists === false) { # Import if not uploaded via web if (!is_uploaded_file($tmp_name)) { if (!@rename($tmp_name, $path)) { Log::error($this->database, __METHOD__, __LINE__, 'Could not move video to uploads'); exit('Error: Could not move video to uploads!'); } } else { if (!@move_uploaded_file($tmp_name, $path)) { Log::error($this->database, __METHOD__, __LINE__, 'Could not move video to uploads'); exit('Error: Could not move video to uploads!'); } } } $info = array(); #Read file info $info = $this->getInfo($path); $info['type'] = $mime_type; # Use title of file $info['title'] = substr(basename($file['name'], $extension), 0, 30); # Size $size = filesize($path) / 1024; if ($size >= 1024) { $info['size'] = round($size / 1024, 1) . ' MB'; } else { $info['size'] = round($size, 1) . ' KB'; } if ($exists === false) { #Create thumb if (!$this->createThumb($path, $video_name, $info['type'], $info['width'], $info['height'])) { Log::error($this->database, __METHOD__, __LINE__, "Could not create thumbnail for video"); exit("Error:Could not create thumbnail for video"); } #Set the thumbnail url $path_thumb = md5($id) . ".jpeg"; } # Save to DB $values = array(LYCHEE_TABLE_PHOTOS, $id, $info['title'], $video_name, $description, $tags, $mime_type, $info['width'], $info['height'], $info['size'], '', '', '', '', '', '', '', $path_thumb, $albumID, $public, $star, $checksum, '', 'video'); $query = Database::prepare($this->database, "INSERT INTO ? (id, title, url, description, tags, type, width, height, size, iso, aperture, make, model, shutter, focal, takestamp, thumbUrl, album, public, star, checksum, medium, media_type) VALUES ('?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?')", $values); $result = $this->database->query($query); if (!$result) { Log::error($this->database, __METHOD__, __LINE__, $this->database->error); exit('Error: Could not save video in database!'); } } # Call plugins $this->plugins(__METHOD__, 1, func_get_args()); return true; }
static function server($albumID = 0, $path) { global $database, $plugins, $settings; # Parse path if (!isset($path)) { $path = LYCHEE_UPLOADS_IMPORT; } if (substr($path, -1) === '/') { $path = substr($path, 0, -1); } if (is_dir($path) === false) { Log::error($database, __METHOD__, __LINE__, 'Given path is not a directory (' . $path . ')'); return 'Error: Given path is not a directory!'; } # Skip folders of Lychee if ($path === LYCHEE_UPLOADS_BIG || $path . '/' === LYCHEE_UPLOADS_BIG || $path === LYCHEE_UPLOADS_MEDIUM || $path . '/' === LYCHEE_UPLOADS_MEDIUM || $path === LYCHEE_UPLOADS_THUMB || $path . '/' === LYCHEE_UPLOADS_THUMB) { Log::error($database, __METHOD__, __LINE__, 'The given path is a reserved path of Lychee (' . $path . ')'); return 'Error: Given path is a reserved path of Lychee!'; } $error = false; $contains['photos'] = false; $contains['albums'] = false; # Invoke plugins directly, as instance method not valid here # Note that updated albumId and path explicitly passed, rather # than using func_get_args() which will only return original ones $plugins->activate(__METHOD__ . ":before", array($albumID, $path)); # Get all files $files = glob($path . '/*'); foreach ($files as $file) { # It is possible to move a file because of directory permissions but # the file may still be unreadable by the user if (!is_readable($file)) { $error = true; Log::error($database, __METHOD__, __LINE__, 'Could not read file or directory: ' . $file); continue; } if (@exif_imagetype($file) !== false) { # Photo if (!Import::photo($database, $plugins, $settings, $file, $albumID)) { $error = true; Log::error($database, __METHOD__, __LINE__, 'Could not import file: ' . $file); continue; } $contains['photos'] = true; } else { if (Video::getMimeType($file) !== false) { # Video if (!Import::video($database, $plugins, $settings, $file, $albumID)) { $error = true; Log::error($database, __METHOD__, __LINE__, 'Could not import file: ' . $file); continue; } $contains['videos'] = true; } else { if (is_dir($file)) { # Folder $name = mysqli_real_escape_string($database, basename($file)); $album = new Album($database, null, null, null); $newAlbumID = $album->add('[Import] ' . $name); $contains['albums'] = true; if ($newAlbumID === false) { $error = true; Log::error($database, __METHOD__, __LINE__, 'Could not create album in Lychee (' . $newAlbumID . ')'); continue; } $import = Import::server($newAlbumID, $file . '/'); if ($import !== true && $import !== 'Notice: Import only contains albums!') { $error = true; Log::error($database, __METHOD__, __LINE__, 'Could not import folder. Function returned warning'); continue; } } } } } # Invoke plugins directly, as instance method not valid here # Note that updated albumId and path explicitly passed, rather # than using func_get_args() which will only return original ones $plugins->activate(__METHOD__ . ":after", array($albumID, $path)); if ($contains['photos'] === false && $contains['videos'] === 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!'; } return true; }