如何在Go后端中支持HEIC/HEIF图像?

huangapple go评论85阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2022年4月5日 02:29:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/71742094.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定