def main(args): if not args.dest.is_dir(): print("Creating", args.dest) args.dest.mkdir(parents=True, exist_ok=True) archived_hashes = load_archived_hashes(args.dest) source_dir = args.source / "wx_emoticons" for filename in source_dir.rglob("**/*"): if filename.is_dir(): continue md5hash = md5sum(filename) if md5hash in archived_hashes: print("Duplicated. Skipped", filename) continue fileext = filetype.guess_extension(str(filename)) if fileext is None: print("Broken. Skipped", filename) continue archived_hashes.add(md5hash) dest_filename = args.dest / "{}.{}".format(md5hash, fileext) print("Storing {} to {}".format(filename, dest_filename)) shutil.copy2(filename, dest_filename)
def upload_image(): if "file" not in request.files: return jsonify(error="File is missing!"), 400 file = request.files["file"] random_string = _get_random_filename() tmp_filepath = os.path.join("/tmp/", random_string) file.save(tmp_filepath) output_type = settings.OUTPUT_TYPE or filetype.guess_extension(tmp_filepath) error = None try: with Image(filename=tmp_filepath) as img: img.strip() with img.convert(output_type) as converted: output_filename = os.path.basename(tmp_filepath) + f".{output_type}" output_path = os.path.join(settings.IMAGES_DIR, output_filename) if output_type not in ["gif"]: converted.sequence = [converted.sequence[0]] converted.save(filename=output_path) except MissingDelegateError: error = "Invalid Filetype" finally: if os.path.exists(tmp_filepath): os.remove(tmp_filepath) if error: return jsonify(error=error), 400 return jsonify(filename=output_filename)
def main(root=cd, dirname=None): if dirname is None: print("please enter dirname, for example: dsHg") return fileDir = os.path.join(os.path.join(root, dirname)) outDir = os.path.join(os.path.join(root, "out")) print("outDir", outDir) utils.removedirs(outDir) for root, dirs, files in os.walk(fileDir): for dir in dirs: # 遍历文件夹 #print(os.path.join(root, dir)) pass for file in files: filePath = os.path.join(root, file) if file.endswith('.' + checkSuffix): guessSuffix = filetype.guess_extension(filePath) if not guessSuffix == checkSuffix: print('{0} error: {1} -> {2}'.format( filePath, checkSuffix, guessSuffix)) outPath = filePath.replace(fileDir, outDir) outPath = outPath.replace('.' + checkSuffix, '.' + guessSuffix) if os.path.exists(filePath): utils.mkdirs(outPath) shutil.copyfile(filePath, outPath)
def from_file(file_name: str): ''' Construct a Track object from a file. ''' path = os.path.join(constant.FILE_PREFIX, file_name) extension = filetype.guess_extension(path) duration, fingerprint = aid.fingerprint_file(path) file_hash = hash_file(path) # Use AcoustID json_resp = aid.lookup(constant.API_KEY, fingerprint, duration) matches = aid.parse_lookup_result(json_resp) try: # Ignore score and recording_id _, _, title, artist = next(matches) except StopIteration: title = 'Track ' + file_hash[:constant.HASH_LEN] artist = 'Unknown' return Track(title, artist, duration, file_hash, fingerprint, extension, path=file_name, local=True)
def pack_image(raw_data, location, sequence=1): ext = filetype.guess_extension(raw_data) if ext is None: raise RuntimeError("Could not guess filetype") filename = filebase(sequence) + '.' + ext write_data(filename, raw_data, location)
def validate(file): file_name = file.raw_filename file_ext = file.raw_filename.split(".")[1] ext_list = [ "mp3", "wav", "wave", "m4a", "mp4", "avi", "mpeg", "mpg", "aif", "aiff", "aifc", "au" ] if (file_ext in ext_list) is False: return { "result": False, "file_name": file_name, "message": file_name + "は音楽や映像ファイルではないようです。" } if os.path.exists(tmp_dir_path) is False: os.mkdir(tmp_dir_path) path = os.path.join(tmp_dir_path, file_name) file.save(path) ext = filetype.guess_extension(path) if file_ext != ext: os.remove(path) return { "result": False, "file_name": file_name, "message": file_name + "は拡張子が偽装されているようです。" } return {"result": True, "file_name": file_name, "message": ""}
def choose(self, event): global imgmark wildcard1 = "All files (*.*)|*.*|" \ "Python source (*.py; *.pyc)|*.py;*.pyc" #文件过滤器 dlg = wx.FileDialog(self, message="Choose a file", defaultFile="", wildcard=wildcard1, style=wx.FD_OPEN | wx.FD_CHANGE_DIR) tmp = "" if dlg.ShowModal() == wx.ID_OK: paths = dlg.GetPaths() #返回的是一个包含路径字符串的列表 tmp = paths[0] #只取第一个文的路径,不做多选 #filename.SetValue(tmp) gusstype = filetype.guess_extension(tmp) print(gusstype) if gusstype in ['jpg', 'png', 'bmp']: print('已选取图片') contents.SetValue("已选取图片\n\n") filename.SetValue(tmp) imgmark = "已选取图片" else: print('请选取格式为jpg、png或bmp格式的图片') contents.SetValue("请选取格式为jpg、png或bmp格式的图片\n\n") else: print('还未选择图片') contents.SetValue('还未选择图片\n\n') dlg.Destroy()
def __init__(self, filename): file = filename self.scale = 1.0 self.scrollX = 0 self.scrollY = 0 self.maxX = 0 self.maxY = 0 self.unscaledX = 0 self.unscaledY = 0 self.manager = None ext = filetype.guess_extension(file) if ext is None: print("Filetype of " + filename + " couldn't be detected, aborting") file = "blank.jpg" ext = "jpg" #os.remove(raw_filename) if ext in ['jfif', 'jpeg', 'jpg', 'JPG', 'png', 'webp']: self.manager = ImageManager(file) else: self.manager = VideoManager(file)
def get_new_vid_name(video): extension = filetype.guess_extension(video) properties = videoprops.get_video_properties(video) vid_date = properties['tags']['creation_time'] formated_vid_date = vid_date.replace("-", "").replace("T", "").replace(":", "") new_name = f"vid_{formated_vid_date[:8]}_{formated_vid_date[8:14]}.{extension}" return new_name
def getFileType(self, path=None): if path == None: return self.__type elif (self.isExist(path) and self.isReadable(path)): if self.isFile(path): value = filetype.guess(path) return value if value != None else Type( None, filetype.guess_extension(path))
def get_file_type(filepath): ''' filetype返回文件类型 该工具通过将文件转化为二进制流,截取文件的二进制的前2或者4字节进行判断文件格式。 :param filepath: :return: str 文件类型 若非支持文件类型,返回None ''' file_type = filetype.guess_extension(filepath) return file_type
def _getFileExtension(file): fileData = bytearray() for c in file.chunks(): fileData += c try: ext = filetype.guess_extension(fileData) return ext except Exception: # todo log return None
def pGetFileExtension(file): rawData = bytearray() for c in file.chunks(): rawData += c try: ext = filetype.guess_extension(rawData) return ext except Exception as e: return None
def test_get_symbiflow_arch_defs_tarball(): from stdm import get_latest_artifact_url import requests import filetype url = get_latest_artifact_url("symbiflow-arch-defs", "install") response = requests.get(url) ext = filetype.guess_extension(response.content) assert "xz" is ext
def __handle_avatar(self, avatar): valid, avatar_data = self.__handle_ressource(avatar) if not valid: print('Invalid avatar', avatar) file_extension = filetype.guess_extension(avatar_data) file_name = avatar.name if file_extension: file_name += '.' + file_extension with self.avatars_folder.open(file_name, 'wb') as f: f.write(avatar_data)
def __handle_attachment(self, attachment): valid, attachment_data = self.__handle_ressource(attachment) if not valid: print('Invalid attachment', attachment) file_extension = filetype.guess_extension(attachment_data) file_name = str(attachment.attachmentId) if file_extension: file_name += '.' + file_extension with self.attachments_folder.open(file_name, 'wb') as f: f.write(attachment_data)
def test_get_symbiflow_arch_defs_tarball(): from stdm import get_latest_artifact_url import requests import filetype urls, build_number = get_latest_artifact_url("symbiflow-arch-defs", "install") url = urls[0]["url"] response = requests.get(url) ext = filetype.guess_extension(response.content) assert "xz" is ext assert urls[0]["name"].endswith("tar.xz") assert isinstance(build_number, int)
def handle_file(self, file_path): self.logger.debug("handle_file %s" % file_path) file_ext = filetype.guess_extension(file_path) if file_ext is None: self.handle_other_file(file_path) return if file_ext == "jpg": self.handle_image_file(file_path) return self.handle_other_file(file_path)
def get_new_img_name(image): extension = filetype.guess_extension(image) image_file = Image.open(image) exif = {} if image_file._getexif() == None: new_name = "-1" else: for tag, value in image_file._getexif().items(): if tag in TAGS: exif[TAGS[tag]] = value pic_date = exif["DateTimeOriginal"] formatted_pic_date = pic_date.replace(":", "").replace(" ", "") new_name = f"img_{formatted_pic_date[:8]}_{formatted_pic_date[8:]}.{extension}" return new_name
def upload_image(): _clear_imagemagick_temp_files() if "file" not in request.files: return jsonify(error="File is missing!"), 400 file = request.files["file"] random_string = _get_random_filename() tmp_filepath = os.path.join("/tmp/", random_string) file.save(tmp_filepath) output_type = settings.OUTPUT_TYPE or filetype.guess_extension( tmp_filepath) error = None output_filename = os.path.basename(tmp_filepath) + f".{output_type}" output_path = os.path.join(settings.IMAGES_DIR, output_filename) try: if os.path.exists(output_path): raise CollisionError with Image(filename=tmp_filepath) as img: img.strip() output_size = img.size if output_type not in ["gif"]: with img.sequence[0] as first_frame: with Image(image=first_frame) as first_frame_img: with first_frame_img.convert(output_type) as converted: converted.save(filename=output_path) else: with img.convert(output_type) as converted: converted.save(filename=output_path) except MissingDelegateError: error = "Invalid Filetype" finally: if os.path.exists(tmp_filepath): os.remove(tmp_filepath) if error: return jsonify(error=error), 400 return jsonify(filename=output_filename, width=output_size[0], height=output_size[1])
def sort_by_type(self, files, file_list, type_list): """Sorts files into type lists.""" hr_list, lr_list, bin = file_list hr_type, lr_type = type_list for file in files: kind = filetype.guess_extension(file) if kind == hr_type: logging.info("Image is High Res: " + file) hr_list.append(file) pass elif kind == lr_type: logging.info("Image is Low Res: " + file) lr_list.append(file) pass else: logging.info("File is Other: " + file) bin.append(file) pass return file_list
def ncm_converter(ncm_path: "str") -> "dict": ncm = pathlib.Path(ncm_path) tmp_name = ncm.with_suffix(".temp") logging.info("Converting %s -> %s", ncm_path, tmp_name) status = {"status": False, "filepath": None, "message": None} try: dump(ncm_path, tmp_name, False) ext = filetype.guess_extension(tmp_name) real_name = tmp_name.rename(ncm.with_suffix(f".{ext}")) status["status"] = True status["filepath"] = real_name logging.info("real filename is %s", real_name) except Exception: err = traceback.format_exc() logging.error("Convert failed for %s -> %s \n%s\n", ncm_path, tmp_name, err) status["error"] = err finally: return status
def _upload_file(self, file: Union[str, bytes], library: str) -> BeautifulSoup: if library == "scitech": self._init_browser() self._browser.open(SCITECH_UPLOAD_URL) elif library == "fiction": self._init_browser() self._browser.open(FICTION_UPLOAD_URL) else: raise ValueError(f"Unknown library to upload to: {library}") if isinstance(file, str): encoder = MultipartEncoder( fields={"file": (basename(file), open(file, "rb"))}) elif isinstance(file, bytes): file_ext = filetype.guess_extension(file) encoder = MultipartEncoder( fields={"file": (f"book.{file_ext}", BytesIO(file))}) with tqdm( desc=basename(file) if isinstance(file, str) else str(file), total=encoder.len, disable=self.show_upload_progress is False, dynamic_ncols=True, unit="B", unit_scale=True, unit_divisor=1024, ) as bar: monitor = MultipartEncoderMonitor( encoder, lambda monitor: bar.update(monitor.bytes_read - bar.n)) session = self._browser.session response = session.post( "https://library.bz/fiction/upload/", data=monitor, headers={"Content-Type": monitor.content_type}, ) response.raise_for_status() self._browser._update_state(response) return BeautifulSoup(response.text, "html.parser")
def generate_new_destination_pic(path, path2, file_name, old_file_name): number = 2 name = file_name.split('.')[0] final_file = path + "\\" + file_name extension = filetype.guess_extension(path2 + "\\" + old_file_name) while 1 == 1: if os.path.exists(final_file): array_file_name = name.split('_') if len(array_file_name) == 3: new_name = f"{array_file_name[0]}_{array_file_name[1]}_{array_file_name[2]}_{number}.{extension}" name = new_name else: number = number + 1 new_name = f"{array_file_name[0]}_{array_file_name[1]}_{array_file_name[2]}_{number}.{extension}" name = new_name final_file = path + "\\" + new_name else: break return final_file
def decrypt_all(args): """Decrypt all .vdata files in source directory. Arguments: args {Array} -- Arguments passed on script execution. """ print(args) check_args(args) vdata_dir = args[1] vdata_result_dir = args[2] vdata_files = glob.glob(os.path.join(vdata_dir, '*.vdata')) for vdata_file in vdata_files: with io.open(vdata_file, 'rb') as reader: # Vaulty adds string "obscured" at the begginign of the image file. # They just change the extesnsion for video files. # This will read first 8 bytes and if the file is tempered with # tempered part will not be read. first_eight = reader.read(8) if first_eight != b'obscured': reader.seek(0) good_bytes = reader.read() # This will provide original file extension. extension = filetype.guess_extension(good_bytes) if extension is None: print(vdata_file + " is unknown file type.") continue # Create destination path. base_file_name = os.path.basename(vdata_file).split('.')[0] new_file_name = 'decrypted_' + base_file_name + '.' + extension dest_path = os.path.join(vdata_result_dir, new_file_name) with io.open(dest_path, 'wb') as writer: writer.write(good_bytes) print(vdata_file + " decrypted.")
def from_b642file(filepath: str, outputfile: str, **kwargs: dict) -> Union[str, None]: """Convert Base64 String to File""" try: file_dir = os.path.dirname(filepath) with open(filepath, 'r') as f: file_data = f.read() if not file_data: return file_bytes = base64.b64decode(file_data) ext = filetype.guess_extension(file_bytes) output_file_path = os.path.join(file_dir, f'{outputfile}.{ext}') with open(output_file_path, 'wb') as f: f.write(file_bytes) except Exception as exc: tc, te, tb = sys.exc_info() print(f'CLASS: {tc}, ERROR: {exc}, LINE_NO: {tb.tb_lineno}') output_file_path = '' return f'{output_file_path}\n'
def test_guess_extension_memoryview(self): buf = memoryview(bytearray([0xFF, 0xD8, 0xFF, 0x00, 0x08])) ext = filetype.guess_extension(buf) self.assertTrue(ext is not None) self.assertEqual(ext, 'jpg')
def test_guess_extension_buffer_invalid(self): buf = bytearray([0xFF, 0x00, 0x00, 0x00, 0x00]) ext = filetype.guess_extension(buf) self.assertTrue(ext is None)
def test_guess_extension_file_path(self): ext = filetype.guess_extension(FIXTURES + '/sample.jpg') self.assertTrue(ext is not None) self.assertEqual(ext, 'jpg')
def mailService(self): email_address = AI.db.email email_password = AI.db.emailpass msg = EmailMessage() self.speak('What is the subject of your message?') msg['Subject'] = self.takeCommand() msg['From'] = email_address self.speak('Who will you like to send the mail to?') receiver = popup('Email', 'Enter Recipient Email Address:') msg['To'] = receiver self.speak( 'Please provide the content of the mail! Would you prefer to type it or say it?' ) while True: response = self.takeCommand() if 'type' in response: content = popup('Email Content', 'Type in the Content of Your Mail:') break elif 'speak' in response: content = self.takeCommand() break else: self.speak( "I couldn't understand your response. Please use either the word type or speak!" ) continue msg.set_content(content) self.speak(('Do you want to attach a file?')) attachcontent = self.takeCommand() if attachcontent.lower() == 'yes': while True: try: self.speak('How many files do you want to attach? ') attachcount = int(self.takeCommand()) attachments = 0 while attachments < attachcount: file_path = fileExplorer() with open(file_path, 'rb') as f: file_data = f.read() file_mime = filetype.guess_mime(f.name) file_type = filetype.guess_extension(f.name) file_name = f.name.split('/')[-1] msg.add_attachment(file_data, maintype=file_mime, subtype=file_type, filename=file_name) attachments += 1 break except ValueError: self.speak('You were meant to type a number.') continue except FileNotFoundError: self.speak('File could not be accessed!') continue except Exception as e: self.speak( "I ran into issues trying to process that. Let's try that again!" ) print(e) continue else: self.speak('Message will be sent without an attachment!') with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp: smtp.login(email_address, email_password) smtp.send_message(msg) self.speak('Your email has been sent!') return