def on_deleted(self, event): super(LocalBoxEventHandler, self).on_deleted(event) if _should_delete_file(event, self.localbox_client): self.localbox_client.delete( get_localbox_path(self.localbox_client.path, event.src_path)) openfiles_ctrl.remove(filesystem_path=os_utils.remove_extension( event.src_path, defaults.LOCALBOX_EXTENSION)) else: getLogger(__name__).debug('on_deleted ignored: %s' % event.src_path)
def populate_filepath_metadata(self, path='/', parent=None): self._should_stop_sync() path = path.lstrip('.').rstrip("/\\") if path == '': fs_path = self.filepath else: fs_path = join(self.filepath, path) getLogger(__name__).info('processing "%s" transformed to "%s"' % (path, fs_path)) is_dir = isdir(fs_path) path_dec = os_utils.remove_extension(path, defaults.LOCALBOX_EXTENSION) fs_path_dec = os_utils.remove_extension(fs_path, defaults.LOCALBOX_EXTENSION) if is_dir: modtime = os.path.getmtime(fs_path) vfsnode = MetaVFS(modtime, path_dec, is_dir) for entry in listdir(fs_path): self.populate_filepath_metadata(join(path, entry), parent=vfsnode) else: if not path.endswith(defaults.LOCALBOX_EXTENSION): if not os.path.exists(fs_path + defaults.LOCALBOX_EXTENSION): modtime = os.path.getmtime(fs_path) else: return else: if os.path.exists(fs_path_dec): modtime_enc = os.path.getmtime(fs_path) modtime_dec = os.path.getmtime(fs_path_dec) modtime = modtime_dec if modtime_dec > modtime_enc else modtime_enc else: modtime = os.path.getmtime(fs_path) vfsnode = MetaVFS(modtime, path_dec, is_dir) if parent is None: self.filepath_metadata = vfsnode else: parent.add_child(vfsnode)
def open_file(data_dic, memory=False): # Get passphrase passphrase = LoginController().get_passphrase(data_dic["label"]) if not passphrase: passphrase = gui_utils.get_user_secret_input( "YourLocalBox - Enter Passphrase", "Please provide the passphrase to unlock file.") if not passphrase: gui_utils.show_error_dialog(_('No passphrase provided. aborting'), 'Error', standalone=True) return None # Stat local box instance localbox_client = LocalBox(data_dic["url"], data_dic["label"], "") # Attempt to decode the file # print data_dic, passphrase try: decoded_contents = localbox_client.decode_file( data_dic["localbox_filename"], data_dic["filename"], passphrase) # If there was a failure, answer wit ha 404 to state that the file doesn't exist except Exception as e: gui_utils.show_error_dialog( _('Failed to decode contents. aborting : {}').format(e), 'Error', standalone=True) getLogger(__name__).info( 'failed to decode contents. aborting : {}'.format(e)) return None # If the file was decoded, write it to disk tmp_decoded_filename = \ os_utils.remove_extension(data_dic["filename"], defaults.LOCALBOX_EXTENSION) getLogger(__name__).info('tmp_decoded_filename: %s' % tmp_decoded_filename) if os.path.exists(tmp_decoded_filename): os.remove(tmp_decoded_filename) if not memory: memory = LocalBoxMemoryFS() if not memory: localfile = open(tmp_decoded_filename, 'wb') localfile.write(decoded_contents) localfile.close() else: tmp_decoded_filename = memory.createfile( tmp_decoded_filename.split('/')[-1], decoded_contents) # Keep file in list of opened files openfiles_ctrl.add(tmp_decoded_filename) return tmp_decoded_filename
def run_file_decryption(filename): try: getLogger(__name__).info('Decrypting and opening file: %s', filename) # verify if the file belongs to any of the configured syncs sync_list = sync_ctrl.list localbox_client = None localbox_filename = None for sync_item in sync_list: getLogger(__name__).debug('sync path: %s' % sync_item.path) sync_path = sync_item.path if sync_item.path.endswith( '/') else sync_item.path + os.path.sep if filename.startswith(sync_path): localbox_filename = os_utils.remove_extension( filename.replace(sync_item.path, ''), defaults.LOCALBOX_EXTENSION) localbox_client = LocalBox(sync_item.url, sync_item.label) break if not localbox_client or not localbox_filename: gui_utils.show_error_dialog( _('%s does not belong to any configured localbox') % filename, 'Error', True) getLogger(__name__).error( '%s does not belong to any configured localbox' % filename) exit(1) # get passphrase label = localbox_client.authenticator.label passphrase = LoginController().get_passphrase(label, remote=True) if not passphrase: gui_wx.ask_passphrase(localbox_client.username, label) passphrase = LoginController().get_passphrase(label, remote=False) if not passphrase: gui_utils.show_error_dialog( _('Failed to get passphrase for label: %s.') % label, 'Error', True) getLogger(__name__).error( 'failed to get passphrase for label: %s. Exiting..' % label) exit(1) # decode file try: decoded_contents = localbox_client.decode_file( localbox_filename, filename, passphrase) except URLError: gui_utils.show_error_dialog(_('Failed to decode contents'), 'Error', standalone=True) getLogger(__name__).info('failed to decode contents. aborting') return 1 # write file tmp_decoded_filename = os_utils.remove_extension( filename, defaults.LOCALBOX_EXTENSION) getLogger(__name__).info('tmp_decoded_filename: %s' % tmp_decoded_filename) if os.path.exists(tmp_decoded_filename): os.remove(tmp_decoded_filename) localfile = open(tmp_decoded_filename, 'wb') localfile.write(decoded_contents) localfile.close() # open file open_file_ext(tmp_decoded_filename) openfiles_ctrl.add(tmp_decoded_filename) getLogger(__name__).info('Finished decrypting and opening file: %s', filename) except Exception as ex: getLogger(__name__).exception(ex)
def run_file_decryption(filename, memory=False): try: getLogger(__name__).info('Decrypting and opening file: %s', filename) # verify if the file belongs to any of the configured syncs sync_list = sync_ctrl.list localbox_client = None localbox_filename = None try: filename = filename.decode('utf-8') except UnicodeEncodeError: # on purpose, it's already an utf-8 string pass for sync_item in sync_list: getLogger(__name__).debug('sync path: %s' % sync_item.path) sync_path = sync_item.path if sync_item.path.endswith( '/') else sync_item.path + os.path.sep if filename.startswith(sync_path): localbox_filename = os_utils.remove_extension( filename.replace(sync_item.path, ''), defaults.LOCALBOX_EXTENSION) localbox_client = LocalBox(sync_item.url, sync_item.label, sync_item.path) break if not localbox_client or not localbox_filename: gui_utils.show_error_dialog( _('%s does not belong to any configured localbox') % filename, 'Error', True) getLogger(__name__).error( '%s does not belong to any configured localbox' % filename) #exit(1) # Request file to be opened data_dic = { "url": sync_item.url, "label": localbox_client.authenticator.label, "filename": filename, "localbox_filename": localbox_filename } name = "LocalBoxApp-{}".format(wx.GetUserId()) instance = wx.SingleInstanceChecker(name) app_is_running = instance.IsAnotherRunning() if app_is_running: file_to_open = Notifs().openFileReq(data_dic) else: file_to_open = open_file(data_dic, memory) if file_to_open is not None and os.path.exists(file_to_open): open_file_ext(file_to_open) getLogger(__name__).info( 'Finished decrypting and opening file: %s', filename) else: gui_utils.show_error_dialog(_('Failed to decode contents'), 'Error', standalone=True) getLogger(__name__).info('failed to decode contents. aborting') except Exception as ex: getLogger(__name__).exception(ex) gui_utils.show_error_dialog(_('Exception {}').format(ex), 'Error', standalone=True)