Skip to content

Instantly share code, notes, and snippets.

@jasich
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasich/1ad1d22969cd17b79889 to your computer and use it in GitHub Desktop.
Save jasich/1ad1d22969cd17b79889 to your computer and use it in GitHub Desktop.
Go Chunks of Base64 Image
image := make([]byte, asset.BlobInfo.Size)
reader := blobstore.NewReader(ctx, asset.BlobInfo.BlobKey)
reader.Read(image)
imageAsBase64 := base64.StdEncoding.EncodeToString(image)
megabyte := (500)
totalSize := len(imageAsBase64)
totalChunks := int(math.Ceil(float64(totalSize) / float64(megabyte)))
chunks := make([]string, totalChunks)
for i := 0; i < totalChunks; i++ {
start := i * megabyte
offset := (start + megabyte)
if offset > totalSize {
offset = totalSize
}
chunks[i] = imageAsBase64[start:offset]
}
for i, chunk := range chunks {
go func(i int, chunk string) {
chunkURL := fmt.Sprintf("%s/%s/Data/%d/.json?auth=%s", firebaseDataURL, share.FriendlyName, i, firebaseAuthKey)
chunkReq, err := http.NewRequest("PUT", chunkURL, strings.NewReader("\""+chunk+"\""))
if err != nil {
//return err
}
chunkReq.Header.Set("Content-Type", "appliction/json")
if _, err := client.Do(chunkReq); err != nil {
//return err
}
}(i, chunk)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment