def index_device(self, device): """Index all new files for a given device""" transfer_dirs = device.get_transfer_dirs() for transfer_dir in transfer_dirs: abstrans = os.path.join(self._basedir, transfer_dir) flagfile = os.path.join(abstrans, '.__indexed__') if os.path.isfile(flagfile): continue for abspath, dirs, files in os.walk(abstrans): for filename in files: devpath = re.sub(r'^%s' % re.escape(abstrans), '', abspath) absfile = os.path.join(abspath, filename) file_model = FileModel.factory(absfile) file_model.add_data({ 'devpath': devpath, 'device': device.id() }) try: file_model.save() except sqlite3.IntegrityError: # unique index on "name", "devpath" and "checksum" duplicates = FileModel.all().where( 'name = ?', file_model.name()).where( 'devpath = ?', file_model.devpath()).where( 'checksum = ?', file_model.checksum()).limit(1) if (len(duplicates) > 0 and duplicates[0].abspath() != file_model.abspath()): # the new file is identical to an old one but not # the same file, lets unlik it. duplicate = os.path.join( file_model.abspath(), file_model.name()) try: os.unlink(duplicate) logger.notice('Removed duplicate %s' % duplicate) try: os.rmdir(file_model.abspath()) except OSError: pass # dir is not empty except OSError: logger.error('Unable to remove duplicate %s' % duplicate) logger.info('%s already exists, skipping..' % os.path.join(devpath, filename)) open(flagfile, 'w').close() # touch indexed flag
def index_device(self, device): """Index all new files for a given device""" transfer_dirs = device.get_transfer_dirs() for transfer_dir in transfer_dirs: abstrans = os.path.join(self._basedir, transfer_dir) flagfile = os.path.join(abstrans, '.__indexed__') if os.path.isfile(flagfile): continue for abspath, dirs, files in os.walk(abstrans): for filename in files: devpath = re.sub(r'^%s' % re.escape(abstrans), '', abspath) absfile = os.path.join(abspath, filename) file_model = FileModel.factory(absfile) file_model.add_data({ 'devpath': devpath, 'device': device.id() }) try: file_model.save() except sqlite3.IntegrityError: # unique index on "name", "devpath" and "checksum" duplicates = FileModel.all().where( 'name = ?', file_model.name()).where( 'devpath = ?', file_model.devpath()).where( 'checksum = ?', file_model.checksum()).limit(1) if (len(duplicates) > 0 and duplicates[0].abspath() != file_model.abspath()): # the new file is identical to an old one but not # the same file, lets unlik it. duplicate = os.path.join(file_model.abspath(), file_model.name()) try: os.unlink(duplicate) logger.notice('Removed duplicate %s' % duplicate) try: os.rmdir(file_model.abspath()) except OSError: pass # dir is not empty except OSError: logger.error('Unable to remove duplicate %s' % duplicate) logger.info('%s already exists, skipping..' % os.path.join(devpath, filename)) open(flagfile, 'w').close() # touch indexed flag
def _harvest(self, filename): """Gather image specific data from 'filename'""" model = self.get_file() image = Image.open(filename) if not image: return self size = image.size if size and len(size) >= 2: model.width(size[0]) model.height(size[1]) exif = {} try: exif = self._get_exif_data(image) except (IOError, AttributeError) as e: logger.notice('Could not read EXIF-data from %s' % filename) return self if 'GPSInfo' in exif: latlng = self._gpsinfo_to_latlng(exif['GPSInfo']) if latlng[0] and latlng[1]: model.latitude(latlng[0]) model.longitude(latlng[1]) if 'DateTime' in exif: date_str = self._clean_exif_string(exif['DateTime']) try: time_struct = time.strptime(date_str, '%Y:%m:%d %H:%M:%S') timestamp = int(time.mktime(time_struct)) if timestamp > 0: model.timestamp(timestamp) except ValueError: logger.notice( 'Could not parse DateTime string "%s" from %s' % (date_str, filename)) if 'Orientation' in exif: model.orientation(exif['Orientation']) return self
def _harvest(self, filename): """Gather image specific data from 'filename'""" model = self.get_file() image = Image.open(filename) if not image: return self size = image.size if size and len(size) >= 2: model.width(size[0]) model.height(size[1]) exif = {} try: exif = self._get_exif_data(image) except (IOError, AttributeError) as e: logger.notice('Could not read EXIF-data from %s' % filename) return self if 'GPSInfo' in exif: latlng = self._gpsinfo_to_latlng(exif['GPSInfo']) if latlng[0] and latlng[1]: model.latitude(latlng[0]) model.longitude(latlng[1]) if 'DateTime' in exif: date_str = self._clean_exif_string(exif['DateTime']) try: time_struct = time.strptime(date_str, '%Y:%m:%d %H:%M:%S') timestamp = int(time.mktime(time_struct)) if timestamp > 0: model.timestamp(timestamp) except ValueError: logger.notice('Could not parse DateTime string "%s" from %s' % (date_str, filename)) if 'Orientation' in exif: model.orientation(exif['Orientation']) return self