def addNewSingleCoverImageFileToDatabaseWithBookId(imageFilename, sqlManager, book_id, title, createTime, tmpLibPath="tmp/", imgLibPath="img/", uploader="", remark="", coverPage=1, lpfs_dll=None): imageFullPath = tmpLibPath + imageFilename image = Image.open(imageFullPath) # type: Image image_width, image_heigh = image.size image.thumbnail([250, 350]) thumbImageName = imageFilename + "-thumb" thumImagePath = tmpLibPath + thumbImageName + ".jpg" thumb_image_width, thumb_image_heigh = image.size if image.mode != "RGB": image = image.convert("RGB") image.save(thumImagePath, "JPEG") image.close() imageFileMd5 = file.calFileMd5(imageFullPath) thumbImageFileMd5 = file.calFileMd5(thumImagePath) image_id = img.mvImageToLib(imageFullPath, imageFileMd5, sqlManager, image_width, image_heigh, imgLibPath=imgLibPath, uploader=uploader, remark=remark) sqlManager.changeTheCover(book_id, image_id, img.IMAGE_TYPE_COVER_CONTENT) thumImage_id = img.mvImageToLib(thumImagePath, thumbImageFileMd5, sqlManager, thumb_image_width, thumb_image_heigh, imgLibPath=imgLibPath, uploader=uploader, remark=remark) sqlManager.changeTheCover(book_id, thumImage_id, img.IMAGE_TYPE_COVER_THUMBILE) sqlManager.sqldb.commit() attr = (c_int * len([image_id, thumImage_id]))(*[image_id, thumImage_id]) lenth_of_attr = c_int(len([image_id, thumImage_id])) lpfs_dll.addImagesToLPFSByImageIds(attr, lenth_of_attr)
def addNewArchFileToImgLibWithProgress(archFilename, sqlManager, title, uploadedArchPath, status, index, tmpLibPath="tmp/", imgLibPath="img/", uploader="", remark="", coverPage=1, lpfs_dll=None): same_title_book_id = sqlManager.title2bookId(title) images_ids_to_added_to_LPFS = [] if same_title_book_id != None: print("The same book title already exists! Error!!") status[index] = "Skip: same book title exists" return False archfileHash = file.calFileMd5(uploadedArchPath + archFilename) status[index] = "Hash calcute error!May be file not exists" if archfileHash == False: return False print("Arch File Hash: " + archfileHash) hashInTable = sqlManager.fileHashInUploadedArchTable(archfileHash) if hashInTable: print("The same hash file has already uploaded.skip this file ") status[index] = "Skip! - same hash file has uploaded already!" return False else: sqlManager.addArchFileHashToTable(archfileHash) same_title_book_id_in_removedtable = sqlManager.title2bookIdInRemoveTable( title) """if same_title_book_id_in_removedtable != None: print("The same book title already exists in removed lib! Error!!") status[index] = "Skip: same book title exists in removed lib" return False""" exactStartTime = time.time() status[index] = "Now exacting..." exactPath = zip.exactArchToTmp(archFilename, uploadedArchPath, status, index) status[index] = "Exacting done." print("压缩包解压耗时%0.10f" % (time.time() - exactStartTime)) if exactPath == None: return filesInTmp = file.getAllImageFiles(exactPath) book_id = sqlManager.getNextBookId() page = 1 forInTmpStartTime = time.time() status[index] = "Now adding pages to system..." sqlManager.cursor.execute(""" CREATE TEMPORARY TABLE IF NOT EXISTS books_tmp AS (SELECT * FROM books where 0); """) ImageFile.LOAD_TRUNCATED_IMAGES = True for f in filesInTmp: startTiem = time.time() imageFileMd5 = file.calFileMd5(f) image = Image.open(f) # type: Image image_width, image_heigh = image.size image.thumbnail([250, 350]) thumbImageName = str(page) + "-thumb" thumImagePath = exactPath + thumbImageName + ".jpg" thumb_image_width, thumb_image_heigh = image.size if image.mode != "RGB": image = image.convert("RGB") image.save(thumImagePath, "JPEG") image.close() thumbImageFileMd5 = file.calFileMd5(thumImagePath) print("图片获取尺寸,生成缩略图部分耗时%0.10f" % (time.time() - startTiem)) startTiem = time.time() image_id = img.mvImageToLib(f, imageFileMd5, sqlManager, image_width, image_heigh, imgLibPath=imgLibPath, uploader=uploader, remark=remark) images_ids_to_added_to_LPFS.append(image_id) print("mvImageToLib耗时%0.10f" % (time.time() - startTiem)) startTiem = time.time() sqlManager.addBookNewPageToSqlNoCommit(book_id, title, image_id, img.IMAGE_TYPE_CONTENT, page) print("Page编入数据库耗时%0.10f" % (time.time() - startTiem)) startTiem = time.time() thumImage_id = img.mvImageToLib(thumImagePath, thumbImageFileMd5, sqlManager, thumb_image_width, thumb_image_heigh, imgLibPath=imgLibPath, uploader=uploader, remark=remark) images_ids_to_added_to_LPFS.append(thumImage_id) print("缩略图mvImageToLib耗时%0.10f" % (time.time() - startTiem)) startTiem = time.time() sqlManager.addBookNewPageToSqlNoCommit(book_id, title, thumImage_id, img.IMAGE_TYPE_CONTENT_THUMBILE, page) print("缩略图Page编入数据库耗时%0.10f" % (time.time() - startTiem)) if page == 1: image_id = img.mvImageToLib(f, imageFileMd5, sqlManager, image_width, image_heigh, imgLibPath=imgLibPath, uploader=uploader, remark=remark) sqlManager.addBookNewPageToSqlNoCommit( book_id, title, image_id, img.IMAGE_TYPE_COVER_CONTENT, page) thumImage_id = img.mvImageToLib(thumImagePath, thumbImageFileMd5, sqlManager, thumb_image_width, thumb_image_heigh, imgLibPath=imgLibPath, uploader=uploader, remark=remark) sqlManager.addBookNewPageToSqlNoCommit( book_id, title, thumImage_id, img.IMAGE_TYPE_COVER_THUMBILE, page) page += 1 status[index] = "Adding pages to system(%d/%d)" % (page, len(filesInTmp)) sqlManager.sqldb.commit() print("forInTmpStartTime耗时%0.10f" % (time.time() - forInTmpStartTime)) seq = c_int32 * len(images_ids_to_added_to_LPFS) attr = seq(*images_ids_to_added_to_LPFS) lenth_of_attr = c_int(len(images_ids_to_added_to_LPFS)) print(byref(attr)) print(lenth_of_attr) lpfs_dll.addImagesToLPFSByImageIds(attr, lenth_of_attr) shutil.rmtree(exactPath) sqlManager.cursor.execute(""" insert into books select * from books_tmp WHERE book_id = %d """ % (book_id)) sqlManager.sqldb.commit() status[index] = "Complete!" return book_id
def addNewArchFileToImgLib(archFilename, sqlManager, title, uploadedArchPath, tmpLibPath="tmp/", imgLibPath="img/", uploader="", remark="", coverPage=1): same_title_book_id = sqlManager.title2bookId(title) if same_title_book_id != None: print("The same book title already exists! Error!!") return False same_title_book_id_in_removedtable = sqlManager.title2bookIdInRemoveTable( title) if same_title_book_id_in_removedtable != None: print("The same book title already exists in removed lib! Error!!") return False exactStartTime = time.time() exactPath = zip.exactArchToTmp(archFilename, uploadedArchPath) print("压缩包解压耗时%0.10f" % (time.time() - exactStartTime)) if exactPath == None: return filesInTmp = file.getAllImageFiles(exactPath) book_id = sqlManager.getNextBookId() page = 1 forInTmpStartTime = time.time() for f in filesInTmp: startTiem = time.time() imageFileMd5 = file.calFileMd5(f) image = Image.open(f) # type: Image image_width, image_heigh = image.size image.thumbnail([250, 350]) thumbImageName = str(page) + "-thumb" thumImagePath = exactPath + "/" + thumbImageName + ".jpg" thumb_image_width, thumb_image_heigh = image.size if image.mode != "RGB": image = image.convert("RGB") image.save(thumImagePath, "JPEG") image.close() thumbImageFileMd5 = file.calFileMd5(thumImagePath) print("图片获取尺寸,生成缩略图部分耗时%0.10f" % (time.time() - startTiem)) startTiem = time.time() image_id = img.mvImageToLib(f, imageFileMd5, sqlManager, image_width, image_heigh, imgLibPath=imgLibPath, uploader=uploader, remark=remark) print("mvImageToLib耗时%0.10f" % (time.time() - startTiem)) startTiem = time.time() sqlManager.addBookNewPageToSqlNoCommit(book_id, title, image_id, img.IMAGE_TYPE_CONTENT, page) print("Page编入数据库耗时%0.10f" % (time.time() - startTiem)) startTiem = time.time() thumImage_id = img.mvImageToLib(thumImagePath, thumbImageFileMd5, sqlManager, thumb_image_width, thumb_image_heigh, imgLibPath=imgLibPath, uploader=uploader, remark=remark) print("缩略图mvImageToLib耗时%0.10f" % (time.time() - startTiem)) startTiem = time.time() sqlManager.addBookNewPageToSqlNoCommit(book_id, title, thumImage_id, img.IMAGE_TYPE_CONTENT_THUMBILE, page) print("缩略图Page编入数据库耗时%0.10f" % (time.time() - startTiem)) if page == 1: image_id = img.mvImageToLib(f, imageFileMd5, sqlManager, image_width, image_heigh, imgLibPath=imgLibPath, uploader=uploader, remark=remark) sqlManager.addBookNewPageToSqlNoCommit( book_id, title, image_id, img.IMAGE_TYPE_COVER_CONTENT, page) thumImage_id = img.mvImageToLib(thumImagePath, thumbImageFileMd5, sqlManager, thumb_image_width, thumb_image_heigh, imgLibPath=imgLibPath, uploader=uploader, remark=remark) sqlManager.addBookNewPageToSqlNoCommit( book_id, title, thumImage_id, img.IMAGE_TYPE_COVER_THUMBILE, page) page += 1 sqlManager.sqldb.commit() print("forInTmpStartTime耗时%0.10f" % (time.time() - forInTmpStartTime)) shutil.rmtree(exactPath) return book_id