{"id":10021071,"date":"2025-11-10T14:52:16","date_gmt":"2025-11-10T06:52:16","guid":{"rendered":"https:\/\/itmax.cn\/?p=10021071"},"modified":"2025-11-10T14:52:16","modified_gmt":"2025-11-10T06:52:16","slug":"python-%e9%80%9a%e8%bf%87%e6%96%87%e4%bb%b6%e5%86%85%e5%ae%b9%e5%88%a4%e6%96%ad%e6%96%87%e4%bb%b6%e7%b1%bb%e5%9e%8b%e7%9a%84%e6%96%b9%e6%a1%88%ef%bc%9a-filetype-%e5%92%8c-magika","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10021071","title":{"rendered":"python \u901a\u8fc7\u6587\u4ef6\u5185\u5bb9\u5224\u65ad\u6587\u4ef6\u7c7b\u578b\u7684\u65b9\u6848\uff1a filetype \u548c Magika"},"content":{"rendered":"<p>\u600e\u4e48\u5224\u65ad\u6587\u4ef6\u7c7b\u578b\uff1f\u901a\u8fc7\u6587\u4ef6\u540d\u540e\u7f00\uff1f\u8fd9\u662f\u5b8c\u5168\u4e0d\u53ef\u9760\u7684\uff0c\u6709\u7edd\u5bf9\u53ef\u9760\u7684\u65b9\u5f0f\u5417\uff1f\u6ca1\u6709<\/p>\n<p>\u76f8\u5bf9\u53ef\u9760\u7684\u65b9\u5f0f\u5c31\u662f\u901a\u8fc7\u6587\u4ef6\u6587\u4ef6\u5185\u5bb9\uff08\u4e8c\u8fdb\u5236\u6d41\uff09\u6765\u5224\u65ad\uff08\u6781\u5c11\u6570\u6587\u4ef6\u7c7b\u578b\u6ca1\u6709\u7279\u6b8a\u7684\u6587\u4ef6\u5934\u6216\u8005\u7279\u5f81\uff0c\u8fd9\u79cd\u65b9\u5f0f\u4e5f\u5224\u65ad\u4e0d\u51fa\u6765\uff09<\/p>\n<p>python \u751f\u6001\u4e0b\uff0c\u6709\u4ec0\u4e48\u5df2\u7ecf\u5c01\u88c5\u597d\u7684\uff0c\u53ef\u4ee5\u901a\u8fc7\u6587\u4ef6\u5185\u5bb9\u5224\u65ad\u6587\u4ef6\u7c7b\u578b\u7684\u5305\u5417\uff1f\u6709\uff0c\u7ecf\u5178\u7684\u5c31\u662f <a href=\"https:\/\/link.segmentfault.com\/?enc=7OB7AFKGxjZDETKzQNJ22w%3D%3D.UifwPjPobdj8D8PBM7t%2B1GaYpeNWNMpkn7PsiYXh5xDqJbP0jPFSp1G8EAeYvEW6\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">filetype<\/a> \uff0c\u4ee5\u53ca\u8c37\u6b4c 2024\u5e74\u4f7f\u7528 AI \u505a\u7684 <a href=\"https:\/\/link.segmentfault.com\/?enc=RvrITJET92qoVG879QUA2Q%3D%3D.HSsvJLSTnOVMtcZOr8Hlx3BbDOlVQ9Hv0Jc6DjK2zu3Wh5dfqTGxG7FVxybgVsMg\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">magika<\/a> <\/p>\n<p>filetype \u7684\u4f18\u70b9\u5c31\u4e0d\u8bf4\u4e86\uff0c\u7f3a\u70b9\u5c31\u662f\u8fd9\u4e2a\u5305\u4ece 2022 \u5e74\u4e4b\u540e\u5c31\u4e0d\u7ef4\u62a4\u4e86: <a href=\"https:\/\/link.segmentfault.com\/?enc=ZuW8nrc%2F%2FQSXQIeWNRpeww%3D%3D.SUB206yynUx1yDmVLCyMo6ycY1xWvvKz9q%2Fx%2Bjuq2U4fI7DDLjBfhGSm2pdMpR5Q\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">https:\/\/pypi.org\/project\/filetype\/#history<\/a><\/p>\n<p>\u81f3\u4e8e Magika \u7684\u60c5\u51b5\uff0c\u53ef\u4ee5\u53c2\u8003\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=x%2BA0RqTQqKC6VrILEIZAJw%3D%3D.t%2BZ8LLy1QhW7qdcY%2BLtfKsB1YOAL%2B%2FBrij7PxpdR2zf%2Bybj%2Bpq4h8ZVcR5daPDt0hF3K8O8IGgwLl4peqPuLlg%3D%3D\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">Rust + AI\uff01\u8c37\u6b4c Magika 1.0 \u628a\u6587\u4ef6\u8bc6\u522b\u63d0\u5347\u5230\u65b0\u9ad8\u5ea6<\/a><\/p>\n<hr>\n<p>\u7136\u540e\u63d0\u4f9b\u4e00\u4e0b\u4f7f\u7528\u6848\u4f8b<\/p>\n<p>filetype \u6848\u4f8b\uff1a\u7528\u6237\u4f20\u8fc7\u6765\u7684\u56fe\u50cf\u9700\u8981\u4fdd\u5b58\u5728 oss \u4e2d\uff0c\u600e\u4e48\u505a\u56fe\u7247\u683c\u5f0f\u5224\u65ad\uff1f\u57fa\u4e8e\u4e0d\u8981\u76f8\u4fe1\u5ba2\u6237\u7aef\u7684\u539f\u5219\uff0c\u5ba2\u6237\u4f20\u8fc7\u6765\u7684 cat.jpg \u5b9e\u9645\u53ef\u80fd\u662f png \u751a\u81f3 gif<\/p>\n<p>\u4e0b\u9762\u63d0\u4f9b\u4e86\u4e00\u4e2a filetype \u7684\u7528\u6cd5\u793a\u4f8b<\/p>\n<p>\u628a\u56fe\u7247\u4e0a\u4f20\u5230 aliyun oss \u7684\u65f6\u5019\uff0c\u9700\u8981\u8bbe\u7f6e content_type \u548c suffix<\/p>\n<pre><code class=\"python\">import filetype\n\n\ndef upload_image(self, stream: bytes, suffix: str) -&gt; str:\n    &quot;&quot;&quot;\n    \u4e0a\u4f20\u56fe\u7247\u5bf9\u8c61\u5230 oss\n    &quot;&quot;&quot;\n    extension = None\n    mime = None\n    result = filetype.guess(stream)\n    if result:\n        extension = result.extension\n        mime = result.mime\n\n    self.upload_stream(stream, content_type=mime,\n                        prefix='xxxxx', suffix=extension)\n\ndef upload_stream(self, stream: bytes, content_type: str | None, prefix: str, suffix: str | None) -&gt; str:\n    &quot;&quot;&quot;\n    \u4e0a\u4f20\u4e8c\u8fdb\u5236\u5bf9\u8c61\u5230 oss\n    &quot;&quot;&quot;\n    hashcode = get_stream_md5(stream)\n    file_like_obj = io.BytesIO(stream)\n\n    oss_file_path: str = f'{prefix}\/{hashcode}' + \/\n        (f'.{suffix}' if suffix else '')\n\n    self.bucket.put_object(\n        oss_file_path,\n        file_like_obj,\n        headers={'Content-Type': content_type} if content_type else None\n    )\n    return oss_file_path<\/code><\/pre>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000047384971\">https:\/\/segmentfault.com\/a\/1190000047384971<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u600e\u4e48\u5224\u65ad\u6587\u4ef6\u7c7b\u578b\uff1f\u901a\u8fc7\u6587\u4ef6\u540d\u540e\u7f00\uff1f\u8fd9\u662f\u5b8c\u5168\u4e0d\u53ef\u9760\u7684\uff0c\u6709\u7edd\u5bf9\u53ef\u9760\u7684\u65b9\u5f0f\u5417\uff1f\u6ca1\u6709 \u76f8\u5bf9\u53ef\u9760\u7684\u65b9\u5f0f\u5c31\u662f\u901a\u8fc7\u6587\u4ef6\u6587\u4ef6\u5185 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10021071","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021071","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10021071"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021071\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10021071"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10021071"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10021071"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}