def upload(dropbox_helper_id, access_token, size, max_retries): from .models import DropboxUploadHelper helper = DropboxUploadHelper.objects.get(id=dropbox_helper_id) def progress_callback(bytes_uploaded, helper=helper, size=size): helper.progress = float(bytes_uploaded) / size helper.save() try: dropbox_path = '/{}'.format(os.path.basename(helper.src)) path_display = upload_to_dropbox(access_token, dropbox_path, helper.src, progress_callback) except Exception as e: helper.failure_reason = str(e) helper.save() couch_user = CouchUser.get_by_username(helper.user.username) if helper.failure_reason is None: dbx = Dropbox(access_token) path_link_metadata = dbx.sharing_create_shared_link_with_settings( path_display, SharedLinkSettings( requested_visibility=RequestedVisibility.team_only, ), ) context = { 'share_url': path_link_metadata.url, 'path': os.path.join( 'Apps', settings.DROPBOX_APP_NAME, path_link_metadata.name, ) } with localize(couch_user.get_language_code()): subject = _('{} has been uploaded to dropbox!'.format(helper.dest)) html_content = render_to_string( 'dropbox/emails/upload_success.html', context) text_content = render_to_string( 'dropbox/emails/upload_success.txt', context) else: context = {'reason': helper.failure_reason, 'path': helper.dest} with localize(couch_user.get_language_code()): subject = _('{} has failed to upload to dropbox'.format( helper.dest)) html_content = render_to_string('dropbox/emails/upload_error.html', context) text_content = render_to_string('dropbox/emails/upload_error.txt', context) send_HTML_email( subject, helper.user.email, html_content, text_content=text_content, )
def upload(dropbox_helper_id, access_token, size, max_retries): from .models import DropboxUploadHelper helper = DropboxUploadHelper.objects.get(id=dropbox_helper_id) def progress_callback(bytes_uploaded, helper=helper, size=size): helper.progress = float(bytes_uploaded) / size helper.save() try: dropbox_path = '/{}'.format(os.path.basename(helper.src)) path_display = upload_to_dropbox(access_token, dropbox_path, helper.src, progress_callback) except Exception as e: helper.failure_reason = str(e) helper.save() couch_user = CouchUser.get_by_username(helper.user.username) if helper.failure_reason is None: dbx = Dropbox(access_token) path_link_metadata = dbx.sharing_create_shared_link_with_settings( path_display, SharedLinkSettings( requested_visibility=RequestedVisibility.team_only, ), ) context = { 'share_url': path_link_metadata.url, 'path': os.path.join( 'Apps', settings.DROPBOX_APP_NAME, path_link_metadata.name, ) } with localize(couch_user.get_language_code()): subject = _('{} has been uploaded to dropbox!'.format(helper.dest)) html_content = render_to_string('dropbox/emails/upload_success.html', context) text_content = render_to_string('dropbox/emails/upload_success.txt', context) else: context = { 'reason': helper.failure_reason, 'path': helper.dest } with localize(couch_user.get_language_code()): subject = _('{} has failed to upload to dropbox'.format(helper.dest)) html_content = render_to_string('dropbox/emails/upload_error.html', context) text_content = render_to_string('dropbox/emails/upload_error.txt', context) send_HTML_email( subject, helper.user.email, html_content, text_content=text_content, )
class DropBoxStorage(Storage): """DropBox Storage class for Django pluggable storage system.""" CHUNK_SIZE = 4 * 1024 * 1024 def __init__(self, oauth2_access_token=None, root_path=None): oauth2_access_token = DROPBOX_OAUTH2_TOKEN self.root_path = DROPBOX_ROOT_PATH if oauth2_access_token is None: raise ImproperlyConfigured("Você deve configurar um token em DROPBOX_OAUTH2_TOKEN ou em settings.py") self.dbx = Dropbox(oauth2_access_token) def user_profile(self): self.dt = self.dbx.users_get_current_account() print(self.dt) def list_dir_and_files_all(self): try: self.dt = self.dbx.files_list_folder(self.root_path) print('DIRETÓRIOS\n') self.list_subdirs(self.dt) except: self.dt = self.dbx.files_get_metadata(self.root_path) print('ARQUIVOS\n') if (isinstance(self.dt, dropbox.files.FileMetadata)): print('FUNCIONA') file = self.list_files(self.dt) return file def list_subdirs(self, dt): for entry in self.dt.entries: i = entry.path_display print(i) def list_files(self): self.dt = self.dbx.files_get_metadata(self.root_path) file = self.dt.path_display print(file) return file def upload_file(self): #print('Uploading para pasta ', DROPBOX_ROOT_PATH_NEW) time = datetime.datetime.now() time = time.strftime("%Y%m%d%H%M%S") FILEPATH = self.simple_backup() with open(FILEPATH, 'rb') as f: self.dbx.files_upload(f.read(), DROPBOX_ROOT_PATH_NEW + '/' + time + '.dump') link = self.dbx.sharing_create_shared_link_with_settings(DROPBOX_ROOT_PATH_NEW + '/' + time + '.dump') url = link.url dl_url = re.sub(r"\?dl\=0", "?dl=1", url) return dl_url def upload_file_compress(self, filename=''): print('Uploading para pasta ', DROPBOX_ROOT_PATH_NEW) t = datetime.datetime.now() t.strftime("%Y%m%d%H%M%S") new_basename = os.path.basename(filename) with open(filename, 'rb') as f: self.dbx.files_upload(f.read(), DROPBOX_ROOT_PATH_NEW + '/' + new_basename) link = self.dbx.sharing_create_shared_link_with_settings( DROPBOX_ROOT_PATH_NEW + '/' + new_basename) url = link.url dl_url = re.sub(r"\?dl\=0", "?dl=1", url) return dl_url def download_file(self, file=''): self.file = (DROPBOX_ROOT_PATH + '/' + file) file_name = self.file.replace('/sistemaweb/backup/', '') print('\nDownloading... /data/backup/' + file_name) try: metadata, res = self.dbx.files_download(self.file) except: pass metadata, res = self.dbx.files_download(file) final_path = ROOT_DIR + '/data/backup/' + file_name f = open(final_path, "wb") f.write(res.content) f.close() if '.zip' in final_path or '.gz' in final_path: print('Arquivo compactado...efetuando descompressão de dados.') self.uncompress_file(final_path) new_basename = os.path.basename(final_path).replace('.gz', '') return new_basename return final_path def list_dirs_root_path(self): self.dt = self.dbx.files_list_folder(self.root_path) #print('ARQUIVOS ENCONTRADOS SERÃO LISTADOS ABAIXO:\n') dir = self.download_file(self.dt.entries[-1].path_display) #self.list_files_root_path(self.dt) return dir def list_files_all(self): self.dt = self.dbx.files_list_folder(self.root_path) #print('ARQUIVOS ENCONTRADOS SERÃO LISTADOS ABAIXO:\n') self.data = [] for entry in self.dt.entries: data = {} data['backup_link'] data['client_modified'] = entry.client_modified data['size'] = str(entry.size)+" bytes" t = entry.client_modified time = datetime.timedelta(hours=2) hora = datetime.datetime.strptime(str(t), '%Y-%m-%d %H:%M:%S') now = hora - time size = entry.size size = str(size)+' bytes' display = entry.path_display print(display, now , size) self.data.append(data) return self.data def simple_backup(self): g = dbbackup.get_connector() execute_from_command_line(["manage.py", "dbbackup", "-v", "1"]) filename = (ROOT_DIR + '/data/backup/' + g.generate_filename()) #print(filename) return filename def compress_file(self, filename='', n=''): if n == '': self.compress_all(filename) elif n == '1': file = self.compress_all(filename) action = self.upload_file_compress(file) return action def compress_all(self, filename): if 'C:' in filename: new_basename = os.path.basename(filename) file = (ROOT_DIR + '/data/backup/' + new_basename + '.gz') f = open(filename, 'rb') data = f.read() f = gzip.open(file, 'wb') f.write(data) f.close() #print('Arquivo compactado com sucesso!!!') return file else: filepath = (ROOT_DIR + '/data/backup/' + filename) file = (ROOT_DIR + '/data/backup/' + filename + '.gz') f = open(filepath, 'rb') data = f.read() f = gzip.open(file, 'wb') f.write(data) f.close() #print('Arquivo compactado com sucesso!!!') return file def uncompress_file(self, filename): if 'C:' in filename: new_basename = os.path.basename(filename).replace('.gz', '') local_file = (ROOT_DIR + '/data/backup/' + new_basename) f = gzip.open(filename, 'rb') data = f.read() f.close() f = open(local_file, 'wb') f.write(data) f.close() print('Arquivo descomprimido com sucesso!!!') else: local_file = (ROOT_DIR + '/data/backup/' + filename) new_basename = os.path.basename(filename).replace('.gz', '') file = (ROOT_DIR + '/data/backup/' + new_basename) f = gzip.open(local_file, 'rb') data = f.read() f.close() f = open(file, 'wb') f.write(data) f.close() print('Arquivo descomprimido com sucesso!!!') def restore_db(self, filepath=''): print("VEJA O FILEPATH: ",filepath) execute_from_command_line(["manage.py", "dbrestore", "-v", "1", "--noinput", "-i", filepath]) def restore(self): # faz a restauração do banco de dados a partir de um backup salvo na dropbox. file_name = DropBoxStorage().list_dirs_root_path() new_basename = file_name if '/' in file_name: basename = shutil.copy(file_name, ROOT_DIR + '/data/backup/') new_basename = os.path.basename(basename) DropBoxStorage().restore_db(new_basename) from django.core.management import call_command import django django.setup() call_command('dbrestore', '-v', '1', "--noinput", "-i", new_basename) #execute_from_command_line(["manage.py", "dbrestore", "-v", "1", "--noinput", "-i", new_basename]) """
def upload(dropbox_helper_id, access_token, size, max_retries): from .models import DropboxUploadHelper helper = DropboxUploadHelper.objects.get(id=dropbox_helper_id) dbx = Dropbox(access_token) try: with open(helper.src, 'rb') as f: chunk = f.read(CHUNK_SIZE) offset = len(chunk) upload_session = dbx.files_upload_session_start(chunk) while True: chunk = f.read(CHUNK_SIZE) if not chunk: break helper.progress = offset / size helper.save() dbx.files_upload_session_append_v2( chunk, UploadSessionCursor( upload_session.session_id, offset, ), ) offset += len(chunk) file_metadata = dbx.files_upload_session_finish( b'', UploadSessionCursor( upload_session.session_id, offset=offset, ), CommitInfo( '/{}'.format(os.path.basename(helper.src)), # When writing the file it won't overwrite an existing file, just add # another file like "filename (2).txt" WriteMode('add'), ), ) except Exception as e: helper.failure_reason = str(e) helper.save() couch_user = CouchUser.get_by_username(helper.user.username) if helper.failure_reason is None: path_link_metadata = dbx.sharing_create_shared_link_with_settings( file_metadata.path_display, SharedLinkSettings( requested_visibility=RequestedVisibility.team_only, ), ) context = { 'share_url': path_link_metadata.url, 'path': os.path.join( u'Apps', settings.DROPBOX_APP_NAME, path_link_metadata.name, ) } with localize(couch_user.get_language_code()): subject = _(u'{} has been uploaded to dropbox!'.format( helper.dest)) html_content = render_to_string( 'dropbox/emails/upload_success.html', context) text_content = render_to_string( 'dropbox/emails/upload_success.txt', context) else: context = {'reason': helper.failure_reason, 'path': helper.dest} with localize(couch_user.get_language_code()): subject = _(u'{} has failed to upload to dropbox'.format( helper.dest)) html_content = render_to_string('dropbox/emails/upload_error.html', context) text_content = render_to_string('dropbox/emails/upload_error.txt', context) send_HTML_email( subject, helper.user.email, html_content, text_content=text_content, )