Fix bug in which only the last photo of a digest was added; Improve mimetype usage by not deleting the subtype

master
voussoir 2017-02-27 23:39:06 -08:00
parent c6615284e0
commit 9ba1a88174
7 changed files with 39 additions and 27 deletions

View File

@ -109,17 +109,18 @@ WARNING_ORDERBY_BADDIRECTION = 'You can\'t order "{column}" by "{direction}". De
# Operational info
EXPRESSION_OPERATORS = {'(', ')', 'OR', 'AND', 'NOT'}
ADDITIONAL_MIMETYPES = {
'srt': 'text',
'mkv': 'video',
'm4a': 'audio',
'7z': 'archive',
'gz': 'archive',
'rar': 'archive',
'tar': 'archive',
'zip': 'archive',
'aac': 'audio/aac',
'ac3': 'audio/ac3',
'dts': 'audio/dts',
'm4a': 'audio/mp4',
'mkv': 'video/x-matroska',
'srt': 'text/plain',
}
DEFAULT_DATADIR = '.\\_etiquette'

View File

@ -138,8 +138,6 @@ def get_mimetype(filepath):
if extension in constants.ADDITIONAL_MIMETYPES:
return constants.ADDITIONAL_MIMETYPES[extension]
mimetype = mimetypes.guess_type(filepath)[0]
if mimetype is not None:
mimetype = mimetype.split('/')[0]
return mimetype
def hyphen_range(s):

View File

@ -369,6 +369,10 @@ class Photo(ObjectBase):
self.thumbnail = row_tuple['thumbnail']
self.mimetype = helpers.get_mimetype(self.real_filepath)
if self.mimetype is None:
self.simple_mimetype = None
else:
self.simple_mimetype = self.mimetype.split('/')[0]
def __reinit__(self):
'''
@ -457,7 +461,7 @@ class Photo(ObjectBase):
return None
return helpers.seconds_to_hms(self.duration)
@decorators.time_me
#@decorators.time_me
def generate_thumbnail(self, *, commit=True, **special):
'''
special:
@ -468,7 +472,7 @@ class Photo(ObjectBase):
#print(hopeful_filepath)
return_filepath = None
if self.mimetype == 'image':
if self.simple_mimetype == 'image':
self.photodb.log.debug('Thumbnailing %s' % self.real_filepath)
try:
image = PIL.Image.open(self.real_filepath)
@ -488,7 +492,7 @@ class Photo(ObjectBase):
image.save(hopeful_filepath, quality=50)
return_filepath = hopeful_filepath
elif self.mimetype == 'video' and constants.ffmpeg:
elif self.simple_mimetype == 'video' and constants.ffmpeg:
#print('video')
probe = constants.ffmpeg.probe(self.real_filepath)
try:
@ -572,7 +576,7 @@ class Photo(ObjectBase):
hopeful_filepath = folder.with_child(basename + '.jpg')
return hopeful_filepath
@decorators.time_me
#@decorators.time_me
def reload_metadata(self, *, commit=True):
'''
Load the file's height, width, etc as appropriate for this type of file.
@ -586,7 +590,7 @@ class Photo(ObjectBase):
self.photodb.log.debug('Reloading metadata for {photo:r}'.format(photo=self))
if self.mimetype == 'image':
if self.simple_mimetype == 'image':
try:
image = PIL.Image.open(self.real_filepath)
except (OSError, ValueError):
@ -594,9 +598,9 @@ class Photo(ObjectBase):
else:
(self.width, self.height) = image.size
image.close()
self.photodb.log.debug('Loaded image data for {photo:r}'.format(photo=self))
#self.photodb.log.debug('Loaded image data for {photo:r}'.format(photo=self))
elif self.mimetype == 'video' and constants.ffmpeg:
elif self.simple_mimetype == 'video' and constants.ffmpeg:
try:
probe = constants.ffmpeg.probe(self.real_filepath)
if probe and probe.video:
@ -606,7 +610,7 @@ class Photo(ObjectBase):
except:
traceback.print_exc()
elif self.mimetype == 'audio' and constants.ffmpeg:
elif self.simple_mimetype == 'audio' and constants.ffmpeg:
try:
probe = constants.ffmpeg.probe(self.real_filepath)
if probe and probe.audio:

View File

@ -421,7 +421,7 @@ class PDBPhotoMixin:
Returns the Photo object.
'''
filename = os.path.abspath(filename)
safeprint.safeprint('Processing %s' % filename)
self.log.debug('New Photo: %s' % filename)
if not os.path.isfile(filename):
raise FileNotFoundError(filename)
@ -754,7 +754,7 @@ class PDBPhotoMixin:
#print('Failed extension_not')
continue
if mimetype and photo.mimetype not in mimetype:
if mimetype and photo.simple_mimetype not in mimetype:
#print('Failed mimetype')
continue
@ -1204,11 +1204,13 @@ class PhotoDB(PDBAlbumMixin, PDBBookmarkMixin, PDBPhotoMixin, PDBTagMixin, PDBUs
albums = {directory.absolute_path: album}
for (current_location, directories, files) in generator:
new_photos = []
for filepath in files:
try:
photo = self.new_photo(filepath.absolute_path, commit=False)
except exceptions.PhotoExists as e:
photo = e.photo
new_photos.append(photo)
if not make_albums:
continue
@ -1223,16 +1225,18 @@ class PhotoDB(PDBAlbumMixin, PDBBookmarkMixin, PDBPhotoMixin, PDBTagMixin, PDBUs
commit=False,
title=current_location.basename,
)
safeprint.safeprint('Created %s' % current_album.title)
self.log.debug('Created %s' % current_album.title)
albums[current_location.absolute_path] = current_album
parent = albums.get(current_location.parent.absolute_path, None)
if parent is not None:
try:
parent.add(current_album, commit=False)
#safeprint.safeprint('Added to %s' % parent.title)
self.log.debug('Added child album to %s' % parent.title)
except exceptions.GroupExists:
pass
self.log.debug('Added photo to %s' % current_album)
for photo in new_photos:
current_album.add_photo(photo, commit=False)
if commit:

View File

@ -122,10 +122,12 @@ def send_file(filepath, override_mimetype=None):
flask.abort(404)
outgoing_headers = {}
print(override_mimetype)
if override_mimetype is not None:
mimetype = override_mimetype
else:
mimetype = mimetypes.guess_type(filepath)[0]
if mimetype is not None:
if 'text/' in mimetype:
mimetype += '; charset=utf-8'

View File

@ -169,7 +169,7 @@
<div id="right">
<!-- THE PHOTO ITSELF -->
<div class="photo_viewer">
{% if photo.mimetype == "image" %}
{% if photo.simple_mimetype == "image" %}
<div id="photo_img_holder">
<img
id="photo_img"
@ -179,9 +179,9 @@
onload="this.style.opacity=0.99"
>
</div>
{% elif photo.mimetype == "video" %}
{% elif photo.simple_mimetype == "video" %}
<video src="{{link}}" controls preload=none {%if photo.thumbnail%}poster="/thumbnail/{{photo.id}}.jpg"{%endif%}></video>
{% elif photo.mimetype == "audio" %}
{% elif photo.simple_mimetype == "audio" %}
<audio src="{{link}}" controls></audio>
{% else %}
<a href="{{link}}">View {{filename}}</a>

View File

@ -1,6 +1,8 @@
{% set thumbnails =
{
"audio": "audio",
"application/zip": "archive",
"application/x-tar": "archive",
"archive": "archive",
"txt": "txt",
"svg": "svg",
@ -26,7 +28,8 @@
{% set choice =
thumbnails.get(photo.extension,
thumbnails.get(photo.mimetype,
'other'))
thumbnails.get(photo.simple_mimetype,
'other')))
%}
src="/static/basic_thumbnails/{{choice}}.png"
{% endif %}