Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix support of raw files #574

Merged
merged 23 commits into from
May 26, 2020
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions app/Http/Controllers/ImportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -362,15 +362,17 @@ public function server_exec(string $path, $albumID, $delete_imported, $force_ski
continue;
}
$extension = Helpers::getExtension($file, true);
if (@exif_imagetype($file) !== false || in_array(strtolower($extension), $this->photoFunctions->validExtensions, true)) {
$raw_formats = strtolower(Configs::get_value('raw_formats', ''));
$is_raw = in_array(strtolower($extension), explode('|', $raw_formats), true);
if (@exif_imagetype($file) !== false || in_array(strtolower($extension), $this->photoFunctions->validExtensions, true) || $is_raw) {
// Photo or Video
if ($this->photo($file, $delete_imported, $albumID, $force_skip_duplicates) === false) {
$this->status_update('Problem: ' . $file . ': Could not import file');
Logs::error(__METHOD__, __LINE__, 'Could not import file (' . $file . ')');
continue;
}
} else {
$this->status_update('Problem: ' . $file . ': Unsupported file type');
$this->status_update('Problem: Unsupported file type (' . $file . ')');
Logs::error(__METHOD__, __LINE__, 'Unsupported file type (' . $file . ')');
continue;
}
Expand Down
13 changes: 13 additions & 0 deletions app/Metadata/Extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Configs;
use App\Logs;
use App\ModelFunctions\Helpers;
use PHPExif\Reader\Reader;

class Extractor
Expand Down Expand Up @@ -71,11 +72,23 @@ public function extract(string $filename, string $type): array
{
$reader = null;

// Get kind of file (photo, video, raw)
$extension = Helpers::getExtension($filename, false);

// check raw files
$is_raw = false;
if (in_array(strtolower($extension), explode('|', Configs::get_value('raw_formats', '')), true)) {
$is_raw = true;
}

if (strpos($type, 'video') !== 0) {
// It's a photo
if (Configs::hasExiftool()) {
// reader with Exiftool adapter
$reader = Reader::factory(Reader::TYPE_EXIFTOOL);
} elseif (Configs::hasImagick() && $is_raw) {
// Use imagick as exif reader for raw files (broader support)
$reader = Reader::factory(Reader::TYPE_IMAGICK);
} else {
// Use Php native tools
$reader = Reader::factory(Reader::TYPE_NATIVE);
Expand Down
79 changes: 64 additions & 15 deletions app/ModelFunctions/PhotoFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,11 @@ public function __construct(Extractor $metadataExtractor, ImageHandlerInterface
*
* @return string
*/
private function file_type($file, string $extension)
public function file_type($file, string $extension)
{
// check raw files
if (in_array(strtolower($extension), explode('|', Configs::get_value('raw_formats', '')), true)) {
$raw_formats = strtolower(Configs::get_value('raw_formats', ''));
if (in_array(strtolower($extension), explode('|', $raw_formats), true)) {
return 'raw';
}

Expand Down Expand Up @@ -282,20 +283,18 @@ public function add(array $file, $albumID_in = 0, $delete_imported = false, $for
}
}

$info = $this->metadataExtractor->extract($path, $mimeType);
if ($kind == 'raw') {
$info = $this->metadataExtractor->bare();
$this->metadataExtractor->size($info, $path);
$this->metadataExtractor->validate($info);
$info['type'] = 'raw';
} else {
$info = $this->metadataExtractor->extract($path, $mimeType);
}

// Use title of file if IPTC title missing
if ($kind == 'raw') {
$info['title'] = substr(basename($file['name']), 0, 98);
} elseif ($info['title'] === '') {
$info['title'] = substr(basename($file['name'], $extension), 0, 98);
if ($info['title'] === '') {
if ($kind == 'raw') {
$info['title'] = substr(basename($file['name']), 0, 98);
} else {
$info['title'] = substr(basename($file['name'], $extension), 0, 98);
}
}

$photo->title = $info['title'];
Expand Down Expand Up @@ -396,8 +395,16 @@ public function add(array $file, $albumID_in = 0, $delete_imported = false, $for
}
}

// Create Thumb
if ($kind == 'raw') {
try {
$frame_tmp = $this->createJpgFromRaw($photo);
} catch (Exception $exception) {
Logs::error(__METHOD__, __LINE__, $exception->getMessage());
}
}

// Create Thumb
if ($kind == 'raw' && $frame_tmp == '') {
$photo->thumbUrl = '';
$photo->thumb2x = 0;
} elseif (!in_array($photo->type, $this->validVideoTypes, true) || $frame_tmp !== '') {
Expand Down Expand Up @@ -446,13 +453,14 @@ public function add(array $file, $albumID_in = 0, $delete_imported = false, $for
*/
public function createSmallerImages(Photo $photo, string $frame_tmp = '')
{
if ($frame_tmp === '') {
if ($frame_tmp === '' || $photo->type == 'raw') {
// Create medium file for normal photos and for raws
$mediumMaxWidth = intval(Configs::get_value('medium_max_width'));
$mediumMaxHeight = intval(Configs::get_value('medium_max_height'));
$this->resizePhoto($photo, 'medium', $mediumMaxWidth, $mediumMaxHeight);
$this->resizePhoto($photo, 'medium', $mediumMaxWidth, $mediumMaxHeight, $frame_tmp);

if (Configs::get_value('medium_2x') === '1') {
$this->resizePhoto($photo, 'medium2x', $mediumMaxWidth * 2, $mediumMaxHeight * 2);
$this->resizePhoto($photo, 'medium2x', $mediumMaxWidth * 2, $mediumMaxHeight * 2, $frame_tmp);
}
}

Expand All @@ -465,6 +473,47 @@ public function createSmallerImages(Photo $photo, string $frame_tmp = '')
}
}

/**
* @param Photo $photo
*
* @return string Path of the jpg file
*/
public function createJpgFromRaw(Photo $photo): string
{
// we need imagick to do the job
if (!Configs::hasImagick()) {
Logs::notice(__METHOD__, __LINE__, 'Saving JPG of raw file to failed: Imagick not installed.');

return '';
}

$filename = $photo->url;
$url = Storage::path('raw/' . $filename);
$ext = pathinfo($filename)['extension'];

// test if Imagaick supports the filetype
if (!in_array($ext, \Imagick::queryformats())) {
Logs::notice(__METHOD__, __LINE__, 'Filetype ' . $ext . ' not supported by Imagick.');

return '';
}

$tmp_file = tempnam(sys_get_temp_dir(), 'lychee') . '.jpeg';
Logs::notice(__METHOD__, __LINE__, 'Saving JPG of raw file to ' . $tmp_file);

$resWidth = $resHeight = 0;
$resWidth = $resHeight = 0;
$width = $photo->width;
$height = $photo->height;
if (!$this->imageHandler->scale($url, $tmp_file, $width, $height, $resWidth, $resHeight)) {
Logs::error(__METHOD__, __LINE__, 'Failed to create JPG from raw file ' . $url . $filename);

return '';
}

return $tmp_file;
}

/**
* @param Photo $photo
*
Expand Down
15 changes: 9 additions & 6 deletions app/Photo.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,9 @@ public function prepareData()

// We need to format the framerate (stored as focal) -> max 2 decimal digits
$photo['focal'] = round($photo['focal'], 2);
} elseif ($this->type == 'raw') {
// It's a raw file -> we also use jpeg as extension
$photoUrl = $this->thumbUrl;
} else {
$photoUrl = $this->url;
}
Expand Down Expand Up @@ -335,7 +338,7 @@ public function prepareData()
$photo['url'] = Storage::url($path_prefix . $this->url);

if ($this->livePhotoUrl !== '' && $this->livePhotoUrl !== null) {
$photo['livePhotoUrl'] = Storage::url($path_prefix . $this->livePhotoUrl);
$photo['livePhotoUrl'] = Storage::url('big/' . $this->livePhotoUrl);
} else {
$photo['livePhotoUrl'] = null;
}
Expand Down Expand Up @@ -488,7 +491,7 @@ public function predelete(bool $keep_original = false)
}
}

if (strpos($this->type, 'video') === 0) {
if ((strpos($this->type, 'video') === 0) || ($this->type == 'raw')) {
$photoName = $this->thumbUrl;
} else {
$photoName = $this->url;
Expand All @@ -501,11 +504,11 @@ public function predelete(bool $keep_original = false)
// TODO: USE STORAGE FOR DELETE
// check first if livePhotoUrl is available
if ($this->livePhotoUrl !== null) {
if (!Storage::exists($path_prefix . $this->livePhotoUrl)) {
Logs::error(__METHOD__, __LINE__, 'Could not find file in ' . Storage::path($path_prefix . $this->livePhotoUrl));
if (!Storage::exists('big/' . $this->livePhotoUrl)) {
Logs::error(__METHOD__, __LINE__, 'Could not find file in ' . Storage::path('big/' . $this->livePhotoUrl));
$error = true;
} elseif (!Storage::delete($path_prefix . $this->livePhotoUrl)) {
Logs::error(__METHOD__, __LINE__, 'Could not delete file in ' . Storage::path($path_prefix . $this->livePhotoUrl));
} elseif (!Storage::delete('big/' . $this->livePhotoUrl)) {
Logs::error(__METHOD__, __LINE__, 'Could not delete file in ' . Storage::path('big/' . $this->livePhotoUrl));
$error = true;
}
}
Expand Down
Loading