CC-1799 Put Airtime Storage into a Human Readable File Naming Convention
can mave a dir in stor and sort files from gui/command line.
This commit is contained in:
parent
5b26f34033
commit
b4b12449ff
|
@ -145,12 +145,14 @@ class MediaMonitor(ProcessEvent):
|
||||||
self.moved_files = {}
|
self.moved_files = {}
|
||||||
self.file_events = deque()
|
self.file_events = deque()
|
||||||
|
|
||||||
self.mask = pyinotify.IN_CREATE | \
|
#self.mask = pyinotify.IN_CREATE | \
|
||||||
pyinotify.IN_MODIFY | \
|
#pyinotify.IN_MODIFY | \
|
||||||
pyinotify.IN_MOVED_FROM | \
|
#pyinotify.IN_MOVED_FROM | \
|
||||||
pyinotify.IN_MOVED_TO | \
|
#pyinotify.IN_MOVED_TO | \
|
||||||
pyinotify.IN_DELETE | \
|
#pyinotify.IN_DELETE | \
|
||||||
pyinotify.IN_DELETE_SELF
|
#pyinotify.IN_DELETE_SELF
|
||||||
|
|
||||||
|
self.mask = pyinotify.ALL_EVENTS
|
||||||
|
|
||||||
self.wm = WatchManager()
|
self.wm = WatchManager()
|
||||||
|
|
||||||
|
@ -206,10 +208,11 @@ class MediaMonitor(ProcessEvent):
|
||||||
if(os.path.exists(filepath)):
|
if(os.path.exists(filepath)):
|
||||||
file_dir = os.path.dirname(filepath)
|
file_dir = os.path.dirname(filepath)
|
||||||
filename = os.path.basename(filepath).split(".")[0]
|
filename = os.path.basename(filepath).split(".")[0]
|
||||||
|
#will be in the format .ext
|
||||||
file_ext = os.path.splitext(filepath)[1]
|
file_ext = os.path.splitext(filepath)[1]
|
||||||
i = 1;
|
i = 1;
|
||||||
while(True):
|
while(True):
|
||||||
new_filepath = "%s/%s(%s).%s" % (file_dir, filename, i, file_ext)
|
new_filepath = "%s/%s(%s)%s" % (file_dir, filename, i, file_ext)
|
||||||
|
|
||||||
if(os.path.exists(new_filepath)):
|
if(os.path.exists(new_filepath)):
|
||||||
i = i+1;
|
i = i+1;
|
||||||
|
@ -225,42 +228,28 @@ class MediaMonitor(ProcessEvent):
|
||||||
try:
|
try:
|
||||||
#get rid of file extention from original name, name might have more than 1 '.' in it.
|
#get rid of file extention from original name, name might have more than 1 '.' in it.
|
||||||
original_name = os.path.basename(imported_filepath)
|
original_name = os.path.basename(imported_filepath)
|
||||||
#self.logger.info('original name: %s', original_name)
|
|
||||||
original_name = original_name.split(".")[0:-1]
|
original_name = original_name.split(".")[0:-1]
|
||||||
#self.logger.info('original name: %s', original_name)
|
|
||||||
original_name = ''.join(original_name)
|
original_name = ''.join(original_name)
|
||||||
#self.logger.info('original name: %s', original_name)
|
|
||||||
|
|
||||||
|
#will be in the format .ext
|
||||||
file_ext = os.path.splitext(imported_filepath)[1]
|
file_ext = os.path.splitext(imported_filepath)[1]
|
||||||
file_info = mutagen.File(imported_filepath, easy=True)
|
md = self.get_mutagen_info(imported_filepath)
|
||||||
|
|
||||||
metadata = {'artist':None,
|
path_md = ['MDATA_KEY_TITLE', 'MDATA_KEY_CREATOR', 'MDATA_KEY_SOURCE', 'MDATA_KEY_TRACKNUMBER', 'MDATA_KEY_BITRATE']
|
||||||
'album':None,
|
|
||||||
'title':None,
|
|
||||||
'tracknumber':None}
|
|
||||||
|
|
||||||
for key in metadata.keys():
|
for m in path_md:
|
||||||
if key in file_info:
|
if m not in md:
|
||||||
metadata[key] = file_info[key][0]
|
md[m] = 'unknown'
|
||||||
|
|
||||||
if metadata['artist'] is not None:
|
filepath = None
|
||||||
base = "%s/%s" % (storage_directory, metadata['artist'])
|
if (md['MDATA_KEY_TITLE'] == 'unknown'):
|
||||||
if metadata['album'] is not None:
|
filepath = "%s/%s/%s/%s-%s%s" % (storage_directory, md['MDATA_KEY_CREATOR'], md['MDATA_KEY_SOURCE'], original_name, md['MDATA_KEY_BITRATE'], file_ext)
|
||||||
base = "%s/%s" % (base, metadata['album'])
|
elif(md['MDATA_KEY_TRACKNUMBER'] == 'unknown'):
|
||||||
if metadata['title'] is not None:
|
filepath = "%s/%s/%s/%s-%s%s" % (storage_directory, md['MDATA_KEY_CREATOR'], md['MDATA_KEY_SOURCE'], md['MDATA_KEY_TITLE'], md['MDATA_KEY_BITRATE'], file_ext)
|
||||||
if metadata['tracknumber'] is not None:
|
|
||||||
metadata['tracknumber'] = "%02d" % (int(metadata['tracknumber']))
|
|
||||||
base = "%s/%s - %s" % (base, metadata['tracknumber'], metadata['title'])
|
|
||||||
else:
|
|
||||||
base = "%s/%s" % (base, metadata['title'])
|
|
||||||
else:
|
|
||||||
base = "%s/%s" % (base, original_name)
|
|
||||||
else:
|
else:
|
||||||
base = "%s/%s" % (storage_directory, original_name)
|
filepath = "%s/%s/%s/%s-%s-%s%s" % (storage_directory, md['MDATA_KEY_CREATOR'], md['MDATA_KEY_SOURCE'], md['MDATA_KEY_TRACKNUMBER'], md['MDATA_KEY_TITLE'], md['MDATA_KEY_BITRATE'], file_ext)
|
||||||
|
|
||||||
base = "%s%s" % (base, file_ext)
|
filepath = self.create_unique_filename(filepath)
|
||||||
|
|
||||||
filepath = self.create_unique_filename(base)
|
|
||||||
self.ensure_dir(filepath)
|
self.ensure_dir(filepath)
|
||||||
|
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
|
@ -279,6 +268,8 @@ class MediaMonitor(ProcessEvent):
|
||||||
if key in attrs :
|
if key in attrs :
|
||||||
md[attrs[key]] = file_info[key][0]
|
md[attrs[key]] = file_info[key][0]
|
||||||
|
|
||||||
|
#md['MDATA_KEY_TRACKNUMBER'] = "%02d" % (int(md['MDATA_KEY_TRACKNUMBER']))
|
||||||
|
|
||||||
md['MDATA_KEY_BITRATE'] = file_info.info.bitrate
|
md['MDATA_KEY_BITRATE'] = file_info.info.bitrate
|
||||||
md['MDATA_KEY_SAMPLERATE'] = file_info.info.sample_rate
|
md['MDATA_KEY_SAMPLERATE'] = file_info.info.sample_rate
|
||||||
md['MDATA_KEY_DURATION'] = self.format_length(file_info.info.length)
|
md['MDATA_KEY_DURATION'] = self.format_length(file_info.info.length)
|
||||||
|
@ -340,8 +331,8 @@ class MediaMonitor(ProcessEvent):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def process_IN_CREATE(self, event):
|
def process_IN_CREATE(self, event):
|
||||||
|
self.logger.info("%s: %s", event.maskname, event.pathname)
|
||||||
if not event.dir:
|
if not event.dir:
|
||||||
self.logger.info("%s: %s", event.maskname, event.pathname)
|
|
||||||
#file created is a tmp file which will be modified and then moved back to the original filename.
|
#file created is a tmp file which will be modified and then moved back to the original filename.
|
||||||
if self.is_temp_file(event.name) :
|
if self.is_temp_file(event.name) :
|
||||||
self.temp_files[event.pathname] = None
|
self.temp_files[event.pathname] = None
|
||||||
|
@ -359,6 +350,10 @@ class MediaMonitor(ProcessEvent):
|
||||||
os.rename(event.pathname, filepath)
|
os.rename(event.pathname, filepath)
|
||||||
self.file_events.append({'mode': MODE_CREATE, 'filepath': filepath})
|
self.file_events.append({'mode': MODE_CREATE, 'filepath': filepath})
|
||||||
|
|
||||||
|
#immediately add a watch on the new directory.
|
||||||
|
else:
|
||||||
|
self.watch_directory(event.pathname)
|
||||||
|
|
||||||
def process_IN_MODIFY(self, event):
|
def process_IN_MODIFY(self, event):
|
||||||
if not event.dir:
|
if not event.dir:
|
||||||
self.logger.info("%s: %s", event.maskname, event.pathname)
|
self.logger.info("%s: %s", event.maskname, event.pathname)
|
||||||
|
@ -370,37 +365,39 @@ class MediaMonitor(ProcessEvent):
|
||||||
|
|
||||||
def process_IN_MOVED_FROM(self, event):
|
def process_IN_MOVED_FROM(self, event):
|
||||||
self.logger.info("%s: %s", event.maskname, event.pathname)
|
self.logger.info("%s: %s", event.maskname, event.pathname)
|
||||||
if event.pathname in self.temp_files:
|
if not event.dir:
|
||||||
del self.temp_files[event.pathname]
|
if event.pathname in self.temp_files:
|
||||||
self.temp_files[event.cookie] = event.pathname
|
del self.temp_files[event.pathname]
|
||||||
else:
|
self.temp_files[event.cookie] = event.pathname
|
||||||
self.moved_files[event.cookie] = event.pathname
|
else:
|
||||||
|
self.moved_files[event.cookie] = event.pathname
|
||||||
|
|
||||||
def process_IN_MOVED_TO(self, event):
|
def process_IN_MOVED_TO(self, event):
|
||||||
self.logger.info("%s: %s", event.maskname, event.pathname)
|
self.logger.info("%s: %s", event.maskname, event.pathname)
|
||||||
if event.cookie in self.temp_files:
|
if not event.dir:
|
||||||
del self.temp_files[event.cookie]
|
if event.cookie in self.temp_files:
|
||||||
self.file_events.append({'filepath': event.pathname, 'mode': MODE_MODIFY})
|
del self.temp_files[event.cookie]
|
||||||
elif event.cookie in self.moved_files:
|
self.file_events.append({'filepath': event.pathname, 'mode': MODE_MODIFY})
|
||||||
old_filepath = self.moved_files[event.cookie]
|
elif event.cookie in self.moved_files:
|
||||||
del self.moved_files[event.cookie]
|
old_filepath = self.moved_files[event.cookie]
|
||||||
|
del self.moved_files[event.cookie]
|
||||||
|
|
||||||
|
global plupload_directory
|
||||||
|
if self.is_parent_directory(old_filepath, plupload_directory):
|
||||||
|
#file renamed from /tmp/plupload does not have a path in our naming scheme yet.
|
||||||
|
md_filepath = self.create_file_path(event.pathname)
|
||||||
|
#move the file a second time to its correct Airtime naming schema.
|
||||||
|
os.rename(event.pathname, md_filepath)
|
||||||
|
self.file_events.append({'filepath': md_filepath, 'mode': MODE_MOVED})
|
||||||
|
else:
|
||||||
|
self.file_events.append({'filepath': event.pathname, 'mode': MODE_MOVED})
|
||||||
|
|
||||||
global plupload_directory
|
|
||||||
if self.is_parent_directory(old_filepath, plupload_directory):
|
|
||||||
#file renamed from /tmp/plupload does not have a path in our naming scheme yet.
|
|
||||||
md_filepath = self.create_file_path(event.pathname)
|
|
||||||
#move the file a second time to its correct Airtime naming schema.
|
|
||||||
os.rename(event.pathname, md_filepath)
|
|
||||||
self.file_events.append({'filepath': md_filepath, 'mode': MODE_MOVED})
|
|
||||||
else:
|
else:
|
||||||
self.file_events.append({'filepath': event.pathname, 'mode': MODE_MOVED})
|
#TODO need to pass in if md5 exists to this file creation function, identical files will just replace current files not have a (1) etc.
|
||||||
|
#file has been most likely dropped into stor folder from an unwatched location. (from gui, mv command not cp)
|
||||||
else:
|
md_filepath = self.create_file_path(event.pathname)
|
||||||
#TODO need to pass in if md5 exists to this file creation function, identical files will just replace current files not have a (1) etc.
|
os.rename(event.pathname, md_filepath)
|
||||||
#file has been most likely dropped into stor folder from an unwatched location. (from gui, mv command not cp)
|
self.file_events.append({'mode': MODE_CREATE, 'filepath': md_filepath})
|
||||||
md_filepath = self.create_file_path(event.pathname)
|
|
||||||
os.rename(event.pathname, md_filepath)
|
|
||||||
self.file_events.append({'mode': MODE_CREATE, 'filepath': md_filepath})
|
|
||||||
|
|
||||||
def process_IN_DELETE(self, event):
|
def process_IN_DELETE(self, event):
|
||||||
if not event.dir:
|
if not event.dir:
|
||||||
|
|
Loading…
Reference in New Issue