def download_video(id, path=None): vs_operator = videostoreoperator.VideoStoreOperator() video_detils = vs_operator.get(id) if path == None: decrypt_path = video_detils.video.name elif os.path.isdir(path): decrypt_path = os.path.join(path, video_detils.video.name) else: decrypt_path = path path = decrypt_path + ".tmp" f = open(path, "wb") count = len(video_detils.chuncks) for i in range(count): chk = video_detils.chuncks[i] buf = download_chunck(chk.path, chk.storagename, chk.container, chk.key) f.write(buf) f.close() if config.is_encrypt(): with open(path, "rb") as in_f, open(decrypt_path, "wb") as out_f: encrypt.decrypt(in_f, out_f, config.config["pwd"]) else: shutil.move(path, decrypt_path) processor = fileprocessor.FileProcessor(decrypt_path) if processor.md5 == video_detils.video.md5: print ("download finished. checksum matched") else: print ("download finished, but checksum doesn't match. Download failed.") processor.close()
def chuncks(self): if self.__chuncks == None: cur_size = 0 if config.is_encrypt(): tmp_path = self.__file_path + ".tmp" with open(self.__file_path, "rb") as in_f, open(tmp_path, "wb") as out_f: encrypt.encrypt(in_f, out_f, config.config["pwd"]) cur_size = os.path.getsize(tmp_path) else: cur_size = self.size if cur_size % FileProcessor.__CHUNCK_SIZE == 0: self.__chuncks = cur_size // FileProcessor.__CHUNCK_SIZE else: self.__chuncks = cur_size // FileProcessor.__CHUNCK_SIZE + 1 return self.__chuncks
def get_chunck(self, index): #use self.chuncks instead of self.__chuncks to forth encryption happened if index < 0 or index >= self.chuncks: return None cur_path = self.__file_path if config.is_encrypt(): cur_path = self.__file_path + ".tmp" f = open(cur_path, "rb") f.seek(index * FileProcessor.__CHUNCK_SIZE) if index == self.chuncks - 1: buf = f.read() else: buf = f.read(FileProcessor.__CHUNCK_SIZE) f.close() return buf
def close(self): if config.is_encrypt() and os.path.exists(self.__file_path + ".tmp"): os.remove(self.__file_path + ".tmp")