mirror of
https://github.com/janeczku/calibre-web
synced 2024-12-26 10:00:37 +00:00
Merge branch 'master' into Develop
# Conflicts: # cps/gdriveutils.py # cps/helper.py
This commit is contained in:
commit
e92497b34e
@ -31,7 +31,6 @@ from cps import config, app
|
|||||||
import cli
|
import cli
|
||||||
import shutil
|
import shutil
|
||||||
from flask import Response, stream_with_context
|
from flask import Response, stream_with_context
|
||||||
|
|
||||||
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 *
|
||||||
@ -189,7 +188,7 @@ def getFolderInFolder(parentId, folderName, drive):
|
|||||||
# drive = getDrive(drive)
|
# drive = getDrive(drive)
|
||||||
query=""
|
query=""
|
||||||
if folderName:
|
if folderName:
|
||||||
query = "title = '%s' and " % folderName.replace("'", "\\'")
|
query = "title = '%s' and " % folderName.replace("'", r"\'")
|
||||||
folder = query + "'%s' in parents and mimeType = 'application/vnd.google-apps.folder'" \
|
folder = query + "'%s' in parents and mimeType = 'application/vnd.google-apps.folder'" \
|
||||||
" and trashed = false" % parentId
|
" and trashed = false" % parentId
|
||||||
fileList = drive.ListFile({'q': folder}).GetList()
|
fileList = drive.ListFile({'q': folder}).GetList()
|
||||||
@ -216,7 +215,7 @@ def getEbooksFolderId(drive=None):
|
|||||||
|
|
||||||
|
|
||||||
def getFile(pathId, fileName, drive):
|
def getFile(pathId, fileName, drive):
|
||||||
metaDataFile = "'%s' in parents and trashed = false and title = '%s'" % (pathId, fileName.replace("'", "\\'"))
|
metaDataFile = "'%s' in parents and trashed = false and title = '%s'" % (pathId, fileName.replace("'", r"\'"))
|
||||||
fileList = drive.ListFile({'q': metaDataFile}).GetList()
|
fileList = drive.ListFile({'q': metaDataFile}).GetList()
|
||||||
if fileList.__len__() == 0:
|
if fileList.__len__() == 0:
|
||||||
return None
|
return None
|
||||||
@ -251,7 +250,7 @@ def getFolderId(path, drive):
|
|||||||
dbChange = True
|
dbChange = True
|
||||||
currentFolderId = currentFolder['id']
|
currentFolderId = currentFolder['id']
|
||||||
else:
|
else:
|
||||||
currentFolderId= None
|
currentFolderId = None
|
||||||
break
|
break
|
||||||
if dbChange:
|
if dbChange:
|
||||||
session.commit()
|
session.commit()
|
||||||
@ -273,16 +272,9 @@ def getFileFromEbooksFolder(path, fileName):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
'''def copyDriveFileRemote(drive, origin_file_id, copy_title):
|
def moveGdriveFileRemote(origin_file_id, new_title):
|
||||||
drive = getDrive(drive)
|
origin_file_id['title']= new_title
|
||||||
copied_file = {'title': copy_title}
|
origin_file_id.Upload()
|
||||||
try:
|
|
||||||
file_data = drive.auth.service.files().copy(
|
|
||||||
fileId = origin_file_id, body=copied_file).execute()
|
|
||||||
return drive.CreateFile({'id': file_data['id']})
|
|
||||||
except errors.HttpError as error:
|
|
||||||
print ('An error occurred: %s' % error)
|
|
||||||
return None'''
|
|
||||||
|
|
||||||
|
|
||||||
# Download metadata.db from gdrive
|
# Download metadata.db from gdrive
|
||||||
@ -294,9 +286,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"})
|
||||||
@ -306,13 +299,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)
|
||||||
# children = drive.auth.service.children().list(folderId=previous_parents).execute()
|
drive.auth.service.files().delete(fileId=previous_parents).execute()
|
||||||
# if not len(children['items']):
|
|
||||||
# drive.auth.service.files().delete(fileId=previous_parents).execute()
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def copyToDrive(drive, uploadFile, createRoot, replaceFiles,
|
def copyToDrive(drive, uploadFile, createRoot, replaceFiles,
|
||||||
@ -325,7 +315,7 @@ def copyToDrive(drive, uploadFile, createRoot, replaceFiles,
|
|||||||
parent = getEbooksFolder(drive)
|
parent = getEbooksFolder(drive)
|
||||||
if os.path.isdir(os.path.join(prevDir,uploadFile)):
|
if os.path.isdir(os.path.join(prevDir,uploadFile)):
|
||||||
existingFolder = drive.ListFile({'q': "title = '%s' and '%s' in parents and trashed = false" %
|
existingFolder = drive.ListFile({'q': "title = '%s' and '%s' in parents and trashed = false" %
|
||||||
(os.path.basename(uploadFile), parent['id'])}).GetList()
|
(os.path.basename(uploadFile).replace("'", r"\'"), parent['id'])}).GetList()
|
||||||
if len(existingFolder) == 0 and (not isInitial or createRoot):
|
if len(existingFolder) == 0 and (not isInitial or createRoot):
|
||||||
parent = drive.CreateFile({'title': os.path.basename(uploadFile),
|
parent = drive.CreateFile({'title': os.path.basename(uploadFile),
|
||||||
'parents': [{"kind": "drive#fileLink", 'id': parent['id']}],
|
'parents': [{"kind": "drive#fileLink", 'id': parent['id']}],
|
||||||
@ -340,11 +330,11 @@ def copyToDrive(drive, uploadFile, createRoot, replaceFiles,
|
|||||||
else:
|
else:
|
||||||
if os.path.basename(uploadFile) not in ignoreFiles:
|
if os.path.basename(uploadFile) not in ignoreFiles:
|
||||||
existingFiles = drive.ListFile({'q': "title = '%s' and '%s' in parents and trashed = false" %
|
existingFiles = drive.ListFile({'q': "title = '%s' and '%s' in parents and trashed = false" %
|
||||||
(os.path.basename(uploadFile), parent['id'])}).GetList()
|
(os.path.basename(uploadFile).replace("'", r"\'"), parent['id'])}).GetList()
|
||||||
if len(existingFiles) > 0:
|
if len(existingFiles) > 0:
|
||||||
driveFile = existingFiles[0]
|
driveFile = existingFiles[0]
|
||||||
else:
|
else:
|
||||||
driveFile = drive.CreateFile({'title': os.path.basename(uploadFile),
|
driveFile = drive.CreateFile({'title': os.path.basename(uploadFile).replace("'", r"\'"),
|
||||||
'parents': [{"kind":"drive#fileLink", 'id': parent['id']}], })
|
'parents': [{"kind":"drive#fileLink", 'id': parent['id']}], })
|
||||||
driveFile.SetContentFile(os.path.join(prevDir, uploadFile))
|
driveFile.SetContentFile(os.path.join(prevDir, uploadFile))
|
||||||
driveFile.Upload()
|
driveFile.Upload()
|
||||||
@ -357,7 +347,7 @@ def uploadFileToEbooksFolder(destFile, f):
|
|||||||
for i, x in enumerate(splitDir):
|
for i, x in enumerate(splitDir):
|
||||||
if i == len(splitDir)-1:
|
if i == len(splitDir)-1:
|
||||||
existingFiles = drive.ListFile({'q': "title = '%s' and '%s' in parents and trashed = false" %
|
existingFiles = drive.ListFile({'q': "title = '%s' and '%s' in parents and trashed = false" %
|
||||||
(x, parent['id'])}).GetList()
|
(x.replace("'", r"\'"), parent['id'])}).GetList()
|
||||||
if len(existingFiles) > 0:
|
if len(existingFiles) > 0:
|
||||||
driveFile = existingFiles[0]
|
driveFile = existingFiles[0]
|
||||||
else:
|
else:
|
||||||
@ -366,7 +356,7 @@ def uploadFileToEbooksFolder(destFile, f):
|
|||||||
driveFile.Upload()
|
driveFile.Upload()
|
||||||
else:
|
else:
|
||||||
existingFolder = drive.ListFile({'q': "title = '%s' and '%s' in parents and trashed = false" %
|
existingFolder = drive.ListFile({'q': "title = '%s' and '%s' in parents and trashed = false" %
|
||||||
(x, parent['id'])}).GetList()
|
(x.replace("'", r"\'"), parent['id'])}).GetList()
|
||||||
if len(existingFolder) == 0:
|
if len(existingFolder) == 0:
|
||||||
parent = drive.CreateFile({'title': x, 'parents': [{"kind": "drive#fileLink", 'id': parent['id']}],
|
parent = drive.CreateFile({'title': x, 'parents': [{"kind": "drive#fileLink", 'id': parent['id']}],
|
||||||
"mimeType": "application/vnd.google-apps.folder"})
|
"mimeType": "application/vnd.google-apps.folder"})
|
||||||
@ -477,9 +467,10 @@ def updateGdriveCalibreFromLocal():
|
|||||||
|
|
||||||
# update gdrive.db on edit of books title
|
# update gdrive.db on edit of books title
|
||||||
def updateDatabaseOnEdit(ID,newPath):
|
def updateDatabaseOnEdit(ID,newPath):
|
||||||
|
sqlCheckPath = newPath if newPath[-1] == '/' else newPath + u'/'
|
||||||
storedPathName = session.query(GdriveId).filter(GdriveId.gdrive_id == ID).first()
|
storedPathName = session.query(GdriveId).filter(GdriveId.gdrive_id == ID).first()
|
||||||
if storedPathName:
|
if storedPathName:
|
||||||
storedPathName.path = newPath
|
storedPathName.path = sqlCheckPath
|
||||||
session.commit()
|
session.commit()
|
||||||
|
|
||||||
|
|
||||||
|
@ -314,11 +314,11 @@ def update_dir_structure_file(book_id, calibrepath, first_author):
|
|||||||
# Rename all files from old names to new names
|
# Rename all files from old names to new names
|
||||||
if authordir != new_authordir or titledir != new_titledir:
|
if authordir != new_authordir or titledir != new_titledir:
|
||||||
try:
|
try:
|
||||||
for file_format in localbook.data:
|
|
||||||
path_name = os.path.join(calibrepath, new_authordir, os.path.basename(path))
|
|
||||||
new_name = get_valid_filename(localbook.title) + ' - ' + get_valid_filename(new_authordir)
|
new_name = get_valid_filename(localbook.title) + ' - ' + get_valid_filename(new_authordir)
|
||||||
|
path_name = os.path.join(calibrepath, new_authordir, os.path.basename(path))
|
||||||
|
for file_format in localbook.data:
|
||||||
os.renames(os.path.join(path_name, file_format.name + '.' + file_format.format.lower()),
|
os.renames(os.path.join(path_name, file_format.name + '.' + file_format.format.lower()),
|
||||||
os.path.join(path_name,new_name + '.' + file_format.format.lower()))
|
os.path.join(path_name, new_name + '.' + file_format.format.lower()))
|
||||||
file_format.name = new_name
|
file_format.name = new_name
|
||||||
except OSError as ex:
|
except OSError as ex:
|
||||||
app.logger.error("Rename file in path " + path + " to " + new_name + ": " + str(ex))
|
app.logger.error("Rename file in path " + path + " to " + new_name + ": " + str(ex))
|
||||||
@ -331,6 +331,7 @@ def update_dir_structure_file(book_id, calibrepath, first_author):
|
|||||||
def update_dir_structure_gdrive(book_id, first_author):
|
def update_dir_structure_gdrive(book_id, first_author):
|
||||||
error = False
|
error = False
|
||||||
book = db.session.query(db.Books).filter(db.Books.id == book_id).first()
|
book = db.session.query(db.Books).filter(db.Books.id == book_id).first()
|
||||||
|
path = book.path
|
||||||
|
|
||||||
authordir = book.path.split('/')[0]
|
authordir = book.path.split('/')[0]
|
||||||
if first_author:
|
if first_author:
|
||||||
@ -338,40 +339,39 @@ def update_dir_structure_gdrive(book_id, first_author):
|
|||||||
else:
|
else:
|
||||||
new_authordir = get_valid_filename(book.authors[0].name)
|
new_authordir = get_valid_filename(book.authors[0].name)
|
||||||
titledir = book.path.split('/')[1]
|
titledir = book.path.split('/')[1]
|
||||||
new_titledir = get_valid_filename(book.title) + " (" + str(book_id) + ")"
|
new_titledir = get_valid_filename(book.title) + u" (" + str(book_id) + u")"
|
||||||
|
|
||||||
if titledir != new_titledir:
|
if titledir != new_titledir:
|
||||||
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()
|
||||||
book.path = book.path.split('/')[0] + '/' + new_titledir
|
book.path = book.path.split('/')[0] + u'/' + new_titledir
|
||||||
|
path = book.path
|
||||||
gd.updateDatabaseOnEdit(gFile['id'], book.path) # only child folder affected
|
gd.updateDatabaseOnEdit(gFile['id'], book.path) # only child folder affected
|
||||||
else:
|
else:
|
||||||
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)
|
||||||
book.path = new_authordir + '/' + book.path.split('/')[1]
|
book.path = new_authordir + u'/' + book.path.split('/')[1]
|
||||||
|
path = book.path
|
||||||
gd.updateDatabaseOnEdit(gFile['id'], book.path)
|
gd.updateDatabaseOnEdit(gFile['id'], book.path)
|
||||||
else:
|
else:
|
||||||
error = _(u'File %(file)s not found on Google Drive', file=authordir) # file not found
|
error = _(u'File %(file)s not found on Google Drive', file=authordir) # file not found
|
||||||
# Rename all files from old names to new names
|
# Rename all files from old names to new names
|
||||||
# ToDo: Rename also all bookfiles with new author name and new title name
|
|
||||||
'''
|
|
||||||
if authordir != new_authordir or titledir != new_titledir:
|
if authordir != new_authordir or titledir != new_titledir:
|
||||||
for format in book.data:
|
new_name = get_valid_filename(book.title) + u' - ' + get_valid_filename(new_authordir)
|
||||||
# path_name = os.path.join(calibrepath, new_authordir, os.path.basename(path))
|
for file_format in book.data:
|
||||||
new_name = get_valid_filename(book.title) + ' - ' + get_valid_filename(book)
|
gFile = gd.getFileFromEbooksFolder(path, file_format.name + u'.' + file_format.format.lower())
|
||||||
format.name = new_name
|
if not gFile:
|
||||||
if gFile:
|
error = _(u'File %(file)s not found on Google Drive', file=file_format.name) # file not found
|
||||||
pass
|
break
|
||||||
else:
|
gd.moveGdriveFileRemote(gFile, new_name + u'.' + file_format.format.lower())
|
||||||
error = _(u'File %(file)s not found on Google Drive', file=format.name) # file not found
|
file_format.name = new_name
|
||||||
break'''
|
|
||||||
return error
|
return error
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user