def CheckTagNotEmpty(tag): (namespace, subtag) = SplitTag(tag) if subtag == '': raise HydrusExceptions.SizeException('Received a zero-length tag!')
def GetMime(path): size = os.path.getsize(path) if size == 0: raise HydrusExceptions.SizeException('File is of zero length!') with open(path, 'rb') as f: f.seek(0) bit_to_check = f.read(256) for (offset, header, mime) in header_and_mime: offset_bit_to_check = bit_to_check[offset:] if offset_bit_to_check.startswith(header): if mime == HC.UNDETERMINED_WM: if HydrusVideoHandling.HasVideoStream(path): return HC.VIDEO_WMV # we'll catch and verify wma later elif mime == HC.UNDETERMINED_PNG: if HydrusVideoHandling.HasVideoStream(path): return HC.IMAGE_APNG else: return HC.IMAGE_PNG else: return mime try: mime = HydrusVideoHandling.GetMime(path) if mime != HC.APPLICATION_UNKNOWN: return mime except HydrusExceptions.MimeException: HydrusData.Print('FFMPEG couldn\'t figure out the mime for: ' + path) except Exception as e: HydrusData.Print('FFMPEG couldn\'t figure out the mime for: ' + path) HydrusData.PrintException(e, do_wait=False) return HC.APPLICATION_UNKNOWN
def GetFileInfo(path, mime=None): size = os.path.getsize(path) if size == 0: raise HydrusExceptions.SizeException('File is of zero length!') if mime is None: mime = GetMime(path) if mime not in HC.ALLOWED_MIMES: raise HydrusExceptions.MimeException('Filetype is not permitted!') width = None height = None duration = None num_frames = None num_words = None if mime in (HC.IMAGE_JPEG, HC.IMAGE_PNG, HC.IMAGE_GIF): ((width, height), duration, num_frames) = HydrusImageHandling.GetImageProperties(path) elif mime == HC.APPLICATION_FLASH: ((width, height), duration, num_frames) = HydrusFlashHandling.GetFlashProperties(path) elif mime in (HC.IMAGE_APNG, HC.VIDEO_AVI, HC.VIDEO_FLV, HC.VIDEO_WMV, HC.VIDEO_MOV, HC.VIDEO_MP4, HC.VIDEO_MKV, HC.VIDEO_WEBM, HC.VIDEO_MPEG): ((width, height), duration, num_frames) = HydrusVideoHandling.GetFFMPEGVideoProperties(path) elif mime == HC.APPLICATION_PDF: num_words = HydrusDocumentHandling.GetPDFNumWords(path) elif mime == HC.AUDIO_MP3: duration = HydrusAudioHandling.GetMP3Duration(path) elif mime == HC.AUDIO_OGG: duration = HydrusAudioHandling.GetOGGVorbisDuration(path) elif mime == HC.AUDIO_FLAC: duration = HydrusAudioHandling.GetFLACDuration(path) elif mime == HC.AUDIO_WMA: duration = HydrusAudioHandling.GetWMADuration(path) return (size, mime, width, height, duration, num_frames, num_words)
def CheckTagNotEmpty(tag): empty_tag = False if tag == '': empty_tag = True if ':' in tag: (namespace, subtag) = tag.split(':', 1) if subtag == '': empty_tag = True if empty_tag: raise HydrusExceptions.SizeException('Received a zero-length tag!')
def CallLater(self, seconds, repeating=False): if not self._window: return if seconds == 0.0: raise HydrusExceptions.SizeException('Cannot set a 0 timer!') self._seconds = seconds self._next_call = HydrusData.GetNowPrecise() + self._seconds self._repeating = repeating self._StartIfNeeded()
def GetFileInfo(path, mime=None): size = os.path.getsize(path) if size == 0: raise HydrusExceptions.SizeException('File is of zero length!') if mime is None: mime = GetMime(path) if mime not in HC.ALLOWED_MIMES: raise HydrusExceptions.MimeException('Filetype is not permitted!') width = None height = None duration = None num_frames = None num_words = None if mime in (HC.IMAGE_JPEG, HC.IMAGE_PNG, HC.IMAGE_GIF): ((width, height), duration, num_frames) = HydrusImageHandling.GetImageProperties(path) elif mime == HC.APPLICATION_FLASH: ((width, height), duration, num_frames) = HydrusFlashHandling.GetFlashProperties(path) elif mime in (HC.IMAGE_APNG, HC.VIDEO_AVI, HC.VIDEO_FLV, HC.VIDEO_WMV, HC.VIDEO_MOV, HC.VIDEO_MP4, HC.VIDEO_MKV, HC.VIDEO_WEBM, HC.VIDEO_MPEG): ((width, height), duration, num_frames) = HydrusVideoHandling.GetFFMPEGVideoProperties(path) elif mime == HC.APPLICATION_PDF: num_words = HydrusDocumentHandling.GetPDFNumWords(path) elif mime in HC.AUDIO: ffmpeg_lines = HydrusVideoHandling.GetFFMPEGInfoLines(path) duration_in_s = HydrusVideoHandling.ParseFFMPEGDuration(ffmpeg_lines) duration = int(duration_in_s * 1000) if width is not None and width < 0: width *= -1 if height is not None and height < 0: width *= -1 if duration is not None and duration < 0: duration *= -1 if num_frames is not None and num_frames < 0: num_frames *= -1 if num_words is not None and num_words < 0: num_words *= -1 return (size, mime, width, height, duration, num_frames, num_words)
def GetMime(path): size = os.path.getsize(path) if size == 0: raise HydrusExceptions.SizeException('File is of zero length!') with open(path, 'rb') as f: f.seek(0) bit_to_check = f.read(256) for (offset, header, mime) in header_and_mime: offset_bit_to_check = bit_to_check[offset:] if offset_bit_to_check.startswith(header): if mime == HC.UNDETERMINED_WM: if HydrusVideoHandling.HasVideoStream(path): return HC.VIDEO_WMV # we'll catch and verify wma later elif mime == HC.UNDETERMINED_PNG: if HydrusVideoHandling.HasVideoStream(path): return HC.IMAGE_APNG else: return HC.IMAGE_PNG else: return mime try: mime = HydrusVideoHandling.GetMime(path) if mime != HC.APPLICATION_UNKNOWN: return mime except HydrusExceptions.MimeException: HydrusData.Print('FFMPEG couldn\'t figure out the mime for: ' + path) except Exception as e: HydrusData.Print('FFMPEG couldn\'t figure out the mime for: ' + path) HydrusData.PrintException(e, do_wait=False) hsaudio_object = hsaudiotag.auto.File(path) if hsaudio_object.valid: if isinstance(hsaudio_object.original, hsaudiotag.mpeg.Mpeg): return HC.AUDIO_MP3 elif isinstance(hsaudio_object.original, hsaudiotag.flac.FLAC): return HC.AUDIO_FLAC elif isinstance(hsaudio_object.original, hsaudiotag.ogg.Vorbis): return HC.AUDIO_OGG elif isinstance(hsaudio_object.original, hsaudiotag.wma.WMADecoder): return HC.AUDIO_WMA return HC.APPLICATION_UNKNOWN
def GetFileInfo(path, mime=None): size = os.path.getsize(path) if size == 0: raise HydrusExceptions.SizeException('File is of zero length!') if mime is None: mime = GetMime(path) if mime not in HC.ALLOWED_MIMES: if mime == HC.TEXT_HTML: raise HydrusExceptions.MimeException( 'Looks like HTML -- maybe the client needs to be taught how to parse this?' ) elif mime == HC.APPLICATION_UNKNOWN: raise HydrusExceptions.MimeException('Unknown filetype!') else: raise HydrusExceptions.MimeException('Filetype is not permitted!') width = None height = None duration = None num_frames = None num_words = None if mime in (HC.IMAGE_JPEG, HC.IMAGE_PNG, HC.IMAGE_GIF): ((width, height), duration, num_frames) = HydrusImageHandling.GetImageProperties(path, mime) elif mime == HC.APPLICATION_FLASH: ((width, height), duration, num_frames) = HydrusFlashHandling.GetFlashProperties(path) elif mime in (HC.IMAGE_APNG, HC.VIDEO_AVI, HC.VIDEO_FLV, HC.VIDEO_WMV, HC.VIDEO_MOV, HC.VIDEO_MP4, HC.VIDEO_MKV, HC.VIDEO_WEBM, HC.VIDEO_MPEG): ((width, height), duration, num_frames) = HydrusVideoHandling.GetFFMPEGVideoProperties(path) elif mime == HC.APPLICATION_PDF: num_words = HydrusDocumentHandling.GetPDFNumWords( path) # this now give None until a better solution can be found elif mime in HC.AUDIO: ffmpeg_lines = HydrusVideoHandling.GetFFMPEGInfoLines(path) duration_in_s = HydrusVideoHandling.ParseFFMPEGDuration(ffmpeg_lines) duration = int(duration_in_s * 1000) if width is not None and width < 0: width *= -1 if height is not None and height < 0: width *= -1 if duration is not None and duration < 0: duration *= -1 if num_frames is not None and num_frames < 0: num_frames *= -1 if num_words is not None and num_words < 0: num_words *= -1 return (size, mime, width, height, duration, num_frames, num_words)