英文:
How to support HEIC/HEIF images in go backend?
问题
我对Go还不太熟悉。我有一些图片上传的代码,我想要上传由iPhone生成的*.heic
格式的图片。我的代码正在执行以下操作:
contentType := http.DetectContentType(fileBytes)
DetectContentType
方法无法识别HEIC
的内容类型,导致默认的内容类型为application/octet-stream
。有没有办法为Go添加对尚未支持的新内容类型的支持呢?
英文:
I am pretty new to Go. I have some image upload code and I am trying to upload a *.heic
image that is produced by iPhones. My code is doing the following:
contentType := http.DetectContentType(fileBytes)
DetectContentType
does not know how to detect content type for HEIC
, which results in default application/octet-stream
content type. Is there a way to add support for new content types that Go does not know how to handle yet?
答案1
得分: 1
http.DetectContentType
函数旨在匹配媒体类型嗅探规范。因此,它不可扩展。
然而,你不需要扩展它:你可以编写自己的函数来检测HEIC,并在数据不匹配HEIC时回退到http.DetectContentType
。
英文:
The http.DetectContentType
function is designed to match the Media Type Sniffing specification. As such, it's not extensible.
However, you don't need to extend it: there's nothing preventing you from writing your own function that detects HEIC, and that falls back to http.DetectContentType
if the data does not match HEIC.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论