Karlom
Karlom

Reputation: 14844

How to extract file extension from base64 encoded file in golang?

I want to allow users to upload files as base64 encoded. The results are like:

data:image/jpeg;base64,/9j/4AAQSkZJRgABAgAAZABkAAD/7AARRHVja3kA...

or

  data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAfAAAAAXNSR0IArs4c6QAAAARnQU...

So I'm wondering what is the idomatic way to extract the file extension from the encoded file strings?

Upvotes: 1

Views: 4564

Answers (2)

Manish Jangir
Manish Jangir

Reputation: 5437

There is a good golang package seems to be there on github https://github.com/vincent-petithory/dataurl that gives whole information about a base64 image URI string.

Upvotes: 0

WaltPurvis
WaltPurvis

Reputation: 1530

It's simple enough to use string/byte functions to extract the mime type — i.e., the part between data: and ;base64.

Then you can use the standard mime package to get the extension from the type:
https://golang.org/pkg/mime/#ExtensionsByType

Upvotes: 2

Related Questions