diff --git a/python_apps/media-monitor/airtimefilemonitor/airtimeprocessevent.py b/python_apps/media-monitor/airtimefilemonitor/airtimeprocessevent.py index de4dedd18..322cd7f0e 100644 --- a/python_apps/media-monitor/airtimefilemonitor/airtimeprocessevent.py +++ b/python_apps/media-monitor/airtimefilemonitor/airtimeprocessevent.py @@ -15,7 +15,7 @@ from airtimefilemonitor.mediaconfig import AirtimeMediaConfig class AirtimeProcessEvent(ProcessEvent): - #TODO + #TODO def my_init(self, queue, airtime_config=None, wm=None, mmc=None): """ Method automatically called from ProcessEvent.__init__(). Additional @@ -25,36 +25,39 @@ class AirtimeProcessEvent(ProcessEvent): self.logger = logging.getLogger() self.config = airtime_config - #put the file path into this dict if we want to ignore certain + #put the file path into this dict if we want to ignore certain #events. For example, when deleting a file from the web ui, we - #are going to delete it from the db on the server side, so media-monitor + #are going to delete it from the db on the server side, so media-monitor #doesn't need to contact the server and tell it to delete again. self.ignore_event = set() - + + self.temp_files = {} self.cookies_IN_MOVED_FROM = {} self.file_events = [] self.multi_queue = queue self.wm = wm self.md_manager = AirtimeMetadata() self.mmc = mmc - + def add_filepath_to_ignore(self, filepath): self.ignore_event.add(filepath) - + #event.dir: True if the event was raised against a directory. #event.name: filename #event.pathname: pathname (str): Concatenation of 'path' and 'name'. def process_IN_CREATE(self, event): self.handle_created_file(event.dir, event.name, event.pathname) - + def handle_created_file(self, dir, name, pathname): if not dir: self.logger.debug("PROCESS_IN_CREATE: %s, name: %s, pathname: %s ", dir, name, pathname) #event is because of a created file - #if self.is_temp_file(name) : + + if self.mmc.is_temp_file(name) : #file created is a tmp file which will be modified and then moved back to the original filename. - #self.temp_files[pathname] = None - if self.mmc.is_audio_file(pathname): + #Easy Tag creates this when changing metadata of ogg files. + self.temp_files[pathname] = None + elif self.mmc.is_audio_file(pathname): if self.mmc.is_parent_directory(pathname, self.config.organize_directory): #file was created in /srv/airtime/stor/organize. Need to process and move #to /srv/airtime/stor/imported @@ -71,11 +74,11 @@ class AirtimeProcessEvent(ProcessEvent): #event is because of a created directory if self.mmc.is_parent_directory(pathname, self.config.storage_directory): self.mmc.set_needed_file_permissions(pathname, dir) - + def process_IN_MODIFY(self, event): self.logger.info("process_IN_MODIFY: %s", event) self.handle_modified_file(event.dir, event.pathname, event.name) - + def handle_modified_file(self, dir, pathname, name): if not dir and self.mmc.is_parent_directory(pathname, self.config.organize_directory): self.logger.info("Modified: %s", pathname) @@ -89,12 +92,14 @@ class AirtimeProcessEvent(ProcessEvent): def process_IN_MOVED_FROM(self, event): self.logger.info("process_IN_MOVED_FROM: %s", event) if not event.dir: - if not self.mmc.is_parent_directory(event.pathname, self.config.organize_directory): + if event.pathname in self.temp_files: + self.temp_files[event.cookie] = event.pathname + elif not self.mmc.is_parent_directory(event.pathname, self.config.organize_directory): #we don't care about moved_from events from the organize dir. if self.mmc.is_audio_file(event.name): self.cookies_IN_MOVED_FROM[event.cookie] = (event, time.time()) - + #Some weird thing to note about this event: it seems that if a file is moved to a newly #created directory, then the IN_MOVED_FROM event will be called, but instead of a corresponding #IN_MOVED_TO event, a IN_CREATED event will happen instead. However if the directory existed before @@ -105,21 +110,24 @@ class AirtimeProcessEvent(ProcessEvent): self.mmc.set_needed_file_permissions(event.pathname, event.dir) if not event.dir: if self.mmc.is_audio_file(event.name): - if event.cookie in self.cookies_IN_MOVED_FROM: + if event.cookie in self.temp_files: + self.file_events.append({'filepath': event.pathname, 'mode': self.config.MODE_MODIFY}) + del self.temp_files[event.cookie] + elif event.cookie in self.cookies_IN_MOVED_FROM: #files original location was also in a watched directory del self.cookies_IN_MOVED_FROM[event.cookie] if self.mmc.is_parent_directory(event.pathname, self.config.organize_directory): filepath = self.mmc.organize_new_file(event.pathname) else: filepath = event.pathname - + if (filepath is not None): self.file_events.append({'filepath': filepath, 'mode': self.config.MODE_MOVED}) else: if self.mmc.is_parent_directory(event.pathname, self.config.organize_directory): self.mmc.organize_new_file(event.pathname) else: - #show dragged from unwatched folder into a watched folder. Do not "organize".:q! + #show dragged from unwatched folder into a watched folder. Do not "organize".:q! if self.mmc.is_parent_directory(event.pathname, self.config.recorded_directory): is_recorded = True else : @@ -144,7 +152,7 @@ class AirtimeProcessEvent(ProcessEvent): def process_IN_DELETE(self, event): self.logger.info("process_IN_DELETE: %s", event) self.handle_removed_file(event.dir, event.pathname) - + def handle_removed_file(self, dir, pathname): self.logger.info("Deleting %s", pathname) if not dir: @@ -154,10 +162,10 @@ class AirtimeProcessEvent(ProcessEvent): elif not self.mmc.is_parent_directory(pathname, self.config.organize_directory): #we don't care if a file was deleted from the organize directory. self.file_events.append({'filepath': pathname, 'mode': self.config.MODE_DELETE}) - + def process_default(self, event): - pass + pass def notifier_loop_callback(self, notifier): if len(self.file_events) > 0: @@ -166,24 +174,24 @@ class AirtimeProcessEvent(ProcessEvent): self.mmc.touch_index_file() self.file_events = [] - + #use items() because we are going to be modifying this #dictionary while iterating over it. for k, pair in self.cookies_IN_MOVED_FROM.items(): event = pair[0] timestamp = pair[1] - + timestamp_now = time.time() - + if timestamp_now - timestamp > 5: #in_moved_from event didn't have a corresponding - #in_moved_to event in the last 5 seconds. - #This means the file was moved to outside of the - #watched directories. Let's handle this by deleting + #in_moved_to event in the last 5 seconds. + #This means the file was moved to outside of the + #watched directories. Let's handle this by deleting #it from the Airtime directory. del self.cookies_IN_MOVED_FROM[k] self.handle_removed_file(False, event.pathname) - + #check for any events recieved from Airtime. try: diff --git a/python_apps/media-monitor/airtimefilemonitor/mediamonitorcommon.py b/python_apps/media-monitor/airtimefilemonitor/mediamonitorcommon.py index 0a26be37c..d3125c5b5 100644 --- a/python_apps/media-monitor/airtimefilemonitor/mediamonitorcommon.py +++ b/python_apps/media-monitor/airtimefilemonitor/mediamonitorcommon.py @@ -21,7 +21,6 @@ class MediaMonitorCommon: directory = os.path.normpath(directory) return (directory == filepath[0:len(directory)]) - """ def is_temp_file(self, filename): info = filename.split(".") @@ -29,7 +28,6 @@ class MediaMonitorCommon: return True else: return False - """ def is_audio_file(self, filename): info = filename.split(".")