pull/826/head
Ozzieisaacs 6 years ago
parent ea1a882353
commit ae7c4908f9

@ -31,13 +31,9 @@ from ub import config
import cli import cli
import shutil import shutil
from flask import Response, stream_with_context from flask import Response, stream_with_context
import time
from sqlalchemy import * from sqlalchemy import *
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import * from sqlalchemy.orm import *
import web import web
class Singleton: class Singleton:
@ -286,9 +282,10 @@ def downloadFile(path, filename, output):
def moveGdriveFolderRemote(origin_file, target_folder): def moveGdriveFolderRemote(origin_file, target_folder):
drive = getDrive(Gdrive.Instance().drive) drive = getDrive(Gdrive.Instance().drive)
previous_parents = ",".join([parent["id"] for parent in origin_file.get('parents')]) previous_parents = ",".join([parent["id"] for parent in origin_file.get('parents')])
children = drive.auth.service.children().list(folderId=previous_parents).execute()
gFileTargetDir = getFileFromEbooksFolder(None, target_folder) gFileTargetDir = getFileFromEbooksFolder(None, target_folder)
if not gFileTargetDir: if not gFileTargetDir:
# Folder is not exisiting, create, and move folder # Folder is not existing, create, and move folder
gFileTargetDir = drive.CreateFile( gFileTargetDir = drive.CreateFile(
{'title': target_folder, 'parents': [{"kind": "drive#fileLink", 'id': getEbooksFolderId()}], {'title': target_folder, 'parents': [{"kind": "drive#fileLink", 'id': getEbooksFolderId()}],
"mimeType": "application/vnd.google-apps.folder"}) "mimeType": "application/vnd.google-apps.folder"})
@ -298,15 +295,10 @@ def moveGdriveFolderRemote(origin_file, target_folder):
addParents=gFileTargetDir['id'], addParents=gFileTargetDir['id'],
removeParents=previous_parents, removeParents=previous_parents,
fields='id, parents').execute() fields='id, parents').execute()
# if previous_parents has no childs anymore, delete originfileparent # if previous_parents has no childs anymore, delete original fileparent
# is not working correctly, because of slow update on gdrive -> could cause trouble in gdrive.db if len(children['items']) == 1:
# (nonexisting folder has id) deleteDatabaseEntry(previous_parents)
time.sleep(20)
children = drive.auth.service.children().list(folderId=previous_parents).execute()
if not len(children['items']):
drive.auth.service.files().delete(fileId=previous_parents).execute() drive.auth.service.files().delete(fileId=previous_parents).execute()
time.sleep(20)
def copyToDrive(drive, uploadFile, createRoot, replaceFiles, def copyToDrive(drive, uploadFile, createRoot, replaceFiles,

@ -22,13 +22,11 @@
import db import db
import ub import ub
from flask import current_app as app from flask import current_app as app
# import logging
from tempfile import gettempdir from tempfile import gettempdir
import sys import sys
import os import os
import re import re
import unicodedata import unicodedata
# from io import BytesIO
import worker import worker
import time import time
from flask import send_from_directory, make_response, redirect, abort from flask import send_from_directory, make_response, redirect, abort
@ -36,16 +34,13 @@ from flask_babel import gettext as _
from flask_login import current_user from flask_login import current_user
from babel.dates import format_datetime from babel.dates import format_datetime
from datetime import datetime from datetime import datetime
# import threading
import shutil import shutil
import requests import requests
# import zipfile
try: try:
import gdriveutils as gd import gdriveutils as gd
except ImportError: except ImportError:
pass pass
import web import web
# import server
import random import random
import subprocess import subprocess
@ -355,9 +350,7 @@ def update_dir_structure_gdrive(book_id, first_author):
gFile = gd.getFileFromEbooksFolder(os.path.dirname(book.path), titledir) gFile = gd.getFileFromEbooksFolder(os.path.dirname(book.path), titledir)
if gFile: if gFile:
gFile['title'] = new_titledir gFile['title'] = new_titledir
gFile.Upload() gFile.Upload()
time.sleep(10)
book.path = book.path.split('/')[0] + u'/' + new_titledir book.path = book.path.split('/')[0] + u'/' + new_titledir
path = book.path path = book.path
gd.updateDatabaseOnEdit(gFile['id'], book.path) # only child folder affected gd.updateDatabaseOnEdit(gFile['id'], book.path) # only child folder affected
@ -365,10 +358,9 @@ def update_dir_structure_gdrive(book_id, first_author):
error = _(u'File %(file)s not found on Google Drive', file=book.path) # file not found error = _(u'File %(file)s not found on Google Drive', file=book.path) # file not found
if authordir != new_authordir: if authordir != new_authordir:
gFile = gd.getFileFromEbooksFolder(os.path.dirname(book.path), titledir) gFile = gd.getFileFromEbooksFolder(os.path.dirname(book.path), new_titledir)
if gFile: if gFile:
gd.moveGdriveFolderRemote(gFile, new_authordir) gd.moveGdriveFolderRemote(gFile, new_authordir)
time.sleep(10)
book.path = new_authordir + u'/' + book.path.split('/')[1] book.path = new_authordir + u'/' + book.path.split('/')[1]
path = book.path path = book.path
gd.updateDatabaseOnEdit(gFile['id'], book.path) gd.updateDatabaseOnEdit(gFile['id'], book.path)

@ -3845,7 +3845,6 @@ def upload():
# upload book to gdrive if nesseccary and add "(bookid)" to folder name # upload book to gdrive if nesseccary and add "(bookid)" to folder name
if config.config_use_google_drive: if config.config_use_google_drive:
gdriveutils.updateGdriveCalibreFromLocal() gdriveutils.updateGdriveCalibreFromLocal()
time.sleep(10)
error = helper.update_dir_stucture(book.id, config.config_calibre_dir) error = helper.update_dir_stucture(book.id, config.config_calibre_dir)
db.session.commit() db.session.commit()
if config.config_use_google_drive: if config.config_use_google_drive:

Loading…
Cancel
Save