I want to send a json string with base64 encoded file in it from a client, basically it looks like this:
{
"data":"aGVscA==",
"filename":"file.txt"
}
And I wrote this struct:
type StoredFile struct {
Data []byte `json:"data"`
Filename string `json:"filename"`
}
Then I decode the json into the struct:
decoder := json.NewDecoder(request.Body)
storedFile := StoredFile{}
err := decoder.Decode(&storedFile)
And save it with gorm:
db.Create(&storedFile)
My question is:
Yes, it considers your b64 string like plain text, because there's no way it can know that it's a base 64 string. So yes again, your size increase will still be there.
The encoding depends on the table. If gorm is simply writing into an already existing table, it will use the encoding that is specified in the structure. For example:
CREATE TABLE `users` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `email` varchar(255) NOT NULL, `password` varchar(255) NOT NULL, `token_user_id` varchar(255) NOT NULL, `is_admin` tinyint(4) NOT NULL DEFAULT '1', PRIMARY KEY (`id`), UNIQUE KEY (email) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
For this table, the CHARSET
is utf8mb4
so it would store using an UTF-8 encoding.
If gorm is also responsible for creating the table however, I believe it uses utf8
by default as the encoding, but I can't seem to find any source to back it up.