Fix bug in which only the last photo of a digest was added; Improve mimetype usage by not deleting the subtype
This commit is contained in:
		
							parent
							
								
									c6615284e0
								
							
						
					
					
						commit
						9ba1a88174
					
				
					 7 changed files with 39 additions and 27 deletions
				
			
		|  | @ -109,17 +109,18 @@ WARNING_ORDERBY_BADDIRECTION = 'You can\'t order "{column}" by "{direction}". De | ||||||
| # Operational info | # Operational info | ||||||
| EXPRESSION_OPERATORS = {'(', ')', 'OR', 'AND', 'NOT'} | EXPRESSION_OPERATORS = {'(', ')', 'OR', 'AND', 'NOT'} | ||||||
| ADDITIONAL_MIMETYPES = { | ADDITIONAL_MIMETYPES = { | ||||||
|     'srt': 'text', |  | ||||||
| 
 |  | ||||||
|     'mkv': 'video', |  | ||||||
| 
 |  | ||||||
|     'm4a': 'audio', |  | ||||||
| 
 |  | ||||||
|     '7z': 'archive', |     '7z': 'archive', | ||||||
|     'gz': 'archive', |     'gz': 'archive', | ||||||
|     'rar': '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' | DEFAULT_DATADIR = '.\\_etiquette' | ||||||
|  |  | ||||||
|  | @ -138,8 +138,6 @@ def get_mimetype(filepath): | ||||||
|     if extension in constants.ADDITIONAL_MIMETYPES: |     if extension in constants.ADDITIONAL_MIMETYPES: | ||||||
|         return constants.ADDITIONAL_MIMETYPES[extension] |         return constants.ADDITIONAL_MIMETYPES[extension] | ||||||
|     mimetype = mimetypes.guess_type(filepath)[0] |     mimetype = mimetypes.guess_type(filepath)[0] | ||||||
|     if mimetype is not None: |  | ||||||
|         mimetype = mimetype.split('/')[0] |  | ||||||
|     return mimetype |     return mimetype | ||||||
| 
 | 
 | ||||||
| def hyphen_range(s): | def hyphen_range(s): | ||||||
|  |  | ||||||
|  | @ -369,6 +369,10 @@ class Photo(ObjectBase): | ||||||
|         self.thumbnail = row_tuple['thumbnail'] |         self.thumbnail = row_tuple['thumbnail'] | ||||||
| 
 | 
 | ||||||
|         self.mimetype = helpers.get_mimetype(self.real_filepath) |         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): |     def __reinit__(self): | ||||||
|         ''' |         ''' | ||||||
|  | @ -457,7 +461,7 @@ class Photo(ObjectBase): | ||||||
|             return None |             return None | ||||||
|         return helpers.seconds_to_hms(self.duration) |         return helpers.seconds_to_hms(self.duration) | ||||||
| 
 | 
 | ||||||
|     @decorators.time_me |     #@decorators.time_me | ||||||
|     def generate_thumbnail(self, *, commit=True, **special): |     def generate_thumbnail(self, *, commit=True, **special): | ||||||
|         ''' |         ''' | ||||||
|         special: |         special: | ||||||
|  | @ -468,7 +472,7 @@ class Photo(ObjectBase): | ||||||
|         #print(hopeful_filepath) |         #print(hopeful_filepath) | ||||||
|         return_filepath = None |         return_filepath = None | ||||||
| 
 | 
 | ||||||
|         if self.mimetype == 'image': |         if self.simple_mimetype == 'image': | ||||||
|             self.photodb.log.debug('Thumbnailing %s' % self.real_filepath) |             self.photodb.log.debug('Thumbnailing %s' % self.real_filepath) | ||||||
|             try: |             try: | ||||||
|                 image = PIL.Image.open(self.real_filepath) |                 image = PIL.Image.open(self.real_filepath) | ||||||
|  | @ -488,7 +492,7 @@ class Photo(ObjectBase): | ||||||
|                 image.save(hopeful_filepath, quality=50) |                 image.save(hopeful_filepath, quality=50) | ||||||
|                 return_filepath = hopeful_filepath |                 return_filepath = hopeful_filepath | ||||||
| 
 | 
 | ||||||
|         elif self.mimetype == 'video' and constants.ffmpeg: |         elif self.simple_mimetype == 'video' and constants.ffmpeg: | ||||||
|             #print('video') |             #print('video') | ||||||
|             probe = constants.ffmpeg.probe(self.real_filepath) |             probe = constants.ffmpeg.probe(self.real_filepath) | ||||||
|             try: |             try: | ||||||
|  | @ -572,7 +576,7 @@ class Photo(ObjectBase): | ||||||
|         hopeful_filepath = folder.with_child(basename + '.jpg') |         hopeful_filepath = folder.with_child(basename + '.jpg') | ||||||
|         return hopeful_filepath |         return hopeful_filepath | ||||||
| 
 | 
 | ||||||
|     @decorators.time_me |     #@decorators.time_me | ||||||
|     def reload_metadata(self, *, commit=True): |     def reload_metadata(self, *, commit=True): | ||||||
|         ''' |         ''' | ||||||
|         Load the file's height, width, etc as appropriate for this type of file. |         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)) |         self.photodb.log.debug('Reloading metadata for {photo:r}'.format(photo=self)) | ||||||
| 
 | 
 | ||||||
|         if self.mimetype == 'image': |         if self.simple_mimetype == 'image': | ||||||
|             try: |             try: | ||||||
|                 image = PIL.Image.open(self.real_filepath) |                 image = PIL.Image.open(self.real_filepath) | ||||||
|             except (OSError, ValueError): |             except (OSError, ValueError): | ||||||
|  | @ -594,9 +598,9 @@ class Photo(ObjectBase): | ||||||
|             else: |             else: | ||||||
|                 (self.width, self.height) = image.size |                 (self.width, self.height) = image.size | ||||||
|                 image.close() |                 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: |             try: | ||||||
|                 probe = constants.ffmpeg.probe(self.real_filepath) |                 probe = constants.ffmpeg.probe(self.real_filepath) | ||||||
|                 if probe and probe.video: |                 if probe and probe.video: | ||||||
|  | @ -606,7 +610,7 @@ class Photo(ObjectBase): | ||||||
|             except: |             except: | ||||||
|                 traceback.print_exc() |                 traceback.print_exc() | ||||||
| 
 | 
 | ||||||
|         elif self.mimetype == 'audio' and constants.ffmpeg: |         elif self.simple_mimetype == 'audio' and constants.ffmpeg: | ||||||
|             try: |             try: | ||||||
|                 probe = constants.ffmpeg.probe(self.real_filepath) |                 probe = constants.ffmpeg.probe(self.real_filepath) | ||||||
|                 if probe and probe.audio: |                 if probe and probe.audio: | ||||||
|  |  | ||||||
|  | @ -421,7 +421,7 @@ class PDBPhotoMixin: | ||||||
|         Returns the Photo object. |         Returns the Photo object. | ||||||
|         ''' |         ''' | ||||||
|         filename = os.path.abspath(filename) |         filename = os.path.abspath(filename) | ||||||
|         safeprint.safeprint('Processing %s' % filename) |         self.log.debug('New Photo: %s' % filename) | ||||||
|         if not os.path.isfile(filename): |         if not os.path.isfile(filename): | ||||||
|             raise FileNotFoundError(filename) |             raise FileNotFoundError(filename) | ||||||
| 
 | 
 | ||||||
|  | @ -754,7 +754,7 @@ class PDBPhotoMixin: | ||||||
|                 #print('Failed extension_not') |                 #print('Failed extension_not') | ||||||
|                 continue |                 continue | ||||||
| 
 | 
 | ||||||
|             if mimetype and photo.mimetype not in mimetype: |             if mimetype and photo.simple_mimetype not in mimetype: | ||||||
|                 #print('Failed mimetype') |                 #print('Failed mimetype') | ||||||
|                 continue |                 continue | ||||||
| 
 | 
 | ||||||
|  | @ -1204,11 +1204,13 @@ class PhotoDB(PDBAlbumMixin, PDBBookmarkMixin, PDBPhotoMixin, PDBTagMixin, PDBUs | ||||||
|             albums = {directory.absolute_path: album} |             albums = {directory.absolute_path: album} | ||||||
| 
 | 
 | ||||||
|         for (current_location, directories, files) in generator: |         for (current_location, directories, files) in generator: | ||||||
|  |             new_photos = [] | ||||||
|             for filepath in files: |             for filepath in files: | ||||||
|                 try: |                 try: | ||||||
|                     photo = self.new_photo(filepath.absolute_path, commit=False) |                     photo = self.new_photo(filepath.absolute_path, commit=False) | ||||||
|                 except exceptions.PhotoExists as e: |                 except exceptions.PhotoExists as e: | ||||||
|                     photo = e.photo |                     photo = e.photo | ||||||
|  |                 new_photos.append(photo) | ||||||
| 
 | 
 | ||||||
|             if not make_albums: |             if not make_albums: | ||||||
|                 continue |                 continue | ||||||
|  | @ -1223,16 +1225,18 @@ class PhotoDB(PDBAlbumMixin, PDBBookmarkMixin, PDBPhotoMixin, PDBTagMixin, PDBUs | ||||||
|                         commit=False, |                         commit=False, | ||||||
|                         title=current_location.basename, |                         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 |                 albums[current_location.absolute_path] = current_album | ||||||
| 
 | 
 | ||||||
|             parent = albums.get(current_location.parent.absolute_path, None) |             parent = albums.get(current_location.parent.absolute_path, None) | ||||||
|             if parent is not None: |             if parent is not None: | ||||||
|                 try: |                 try: | ||||||
|                     parent.add(current_album, commit=False) |                     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: |                 except exceptions.GroupExists: | ||||||
|                     pass |                     pass | ||||||
|  |                 self.log.debug('Added photo to %s' % current_album) | ||||||
|  |                 for photo in new_photos: | ||||||
|                     current_album.add_photo(photo, commit=False) |                     current_album.add_photo(photo, commit=False) | ||||||
| 
 | 
 | ||||||
|         if commit: |         if commit: | ||||||
|  |  | ||||||
|  | @ -122,10 +122,12 @@ def send_file(filepath, override_mimetype=None): | ||||||
|         flask.abort(404) |         flask.abort(404) | ||||||
| 
 | 
 | ||||||
|     outgoing_headers = {} |     outgoing_headers = {} | ||||||
|  |     print(override_mimetype) | ||||||
|     if override_mimetype is not None: |     if override_mimetype is not None: | ||||||
|         mimetype = override_mimetype |         mimetype = override_mimetype | ||||||
|     else: |     else: | ||||||
|         mimetype = mimetypes.guess_type(filepath)[0] |         mimetype = mimetypes.guess_type(filepath)[0] | ||||||
|  | 
 | ||||||
|     if mimetype is not None: |     if mimetype is not None: | ||||||
|         if 'text/' in mimetype: |         if 'text/' in mimetype: | ||||||
|             mimetype += '; charset=utf-8' |             mimetype += '; charset=utf-8' | ||||||
|  |  | ||||||
|  | @ -169,7 +169,7 @@ | ||||||
| <div id="right"> | <div id="right"> | ||||||
|     <!-- THE PHOTO ITSELF --> |     <!-- THE PHOTO ITSELF --> | ||||||
|     <div class="photo_viewer"> |     <div class="photo_viewer"> | ||||||
|         {% if photo.mimetype == "image" %} |         {% if photo.simple_mimetype == "image" %} | ||||||
|         <div id="photo_img_holder"> |         <div id="photo_img_holder"> | ||||||
|             <img |             <img | ||||||
|             id="photo_img" |             id="photo_img" | ||||||
|  | @ -179,9 +179,9 @@ | ||||||
|             onload="this.style.opacity=0.99" |             onload="this.style.opacity=0.99" | ||||||
|             > |             > | ||||||
|         </div> |         </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> |         <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> |         <audio src="{{link}}" controls></audio> | ||||||
|         {% else %} |         {% else %} | ||||||
|         <a href="{{link}}">View {{filename}}</a> |         <a href="{{link}}">View {{filename}}</a> | ||||||
|  |  | ||||||
|  | @ -1,6 +1,8 @@ | ||||||
| {% set thumbnails = | {% set thumbnails = | ||||||
|     { |     { | ||||||
|         "audio": "audio", |         "audio": "audio", | ||||||
|  |         "application/zip": "archive", | ||||||
|  |         "application/x-tar": "archive", | ||||||
|         "archive": "archive", |         "archive": "archive", | ||||||
|         "txt": "txt", |         "txt": "txt", | ||||||
|         "svg": "svg", |         "svg": "svg", | ||||||
|  | @ -26,7 +28,8 @@ | ||||||
|                 {% set choice = |                 {% set choice = | ||||||
|                     thumbnails.get(photo.extension, |                     thumbnails.get(photo.extension, | ||||||
|                     thumbnails.get(photo.mimetype, |                     thumbnails.get(photo.mimetype, | ||||||
|                     'other')) |                     thumbnails.get(photo.simple_mimetype, | ||||||
|  |                     'other'))) | ||||||
|                 %} |                 %} | ||||||
|                 src="/static/basic_thumbnails/{{choice}}.png" |                 src="/static/basic_thumbnails/{{choice}}.png" | ||||||
|             {% endif %} |             {% endif %} | ||||||
|  |  | ||||||
		Loading…
	
		Reference in a new issue