Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

imagebundler: treat octet streams with svg signature as svg mimetypes #2370

Merged
merged 2 commits into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ci/release/changelogs/next.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
- Compiler:
- fixes panic when `sql_shape` shape value had mixed casing [#2349](https://github.com/terrastruct/d2/pull/2349)
- fixes support for `center` in `d2-config` [#2360](https://github.com/terrastruct/d2/pull/2360)
- CLI: fetch and render remote images of mimetype octet-stream correctly [#2370](https://github.com/terrastruct/d2/pull/2370)
17 changes: 14 additions & 3 deletions lib/imgbundler/imgbundler.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func worker(ctx context.Context, l simplelog.Logger, inputPath string, href []by
var err error
if isRemote {
l.Debug(fmt.Sprintf("fetching %s remotely", string(href)))
buf, mimeType, err = httpGet(ctx, html.UnescapeString(string(href)))
buf, mimeType, err = httpGet(ctx, l, html.UnescapeString(string(href)))
} else {
l.Debug(fmt.Sprintf("reading %s from disk", string(href)))
path := html.UnescapeString(string(href))
Expand All @@ -181,8 +181,15 @@ func worker(ctx context.Context, l simplelog.Logger, inputPath string, href []by

if mimeType == "" {
mimeType = sniffMimeType(href, buf, isRemote)
l.Debug(fmt.Sprintf("no mimetype provided - sniffed MIME type for %s: %s", string(href), mimeType))
} else {
l.Debug(fmt.Sprintf("mimetype provided for %s: %s", string(href), mimeType))
}
mimeType = strings.Replace(mimeType, "text/xml", "image/svg+xml", 1)
if mimeType == "application/octet-stream" && bytes.Contains(buf, []byte("<svg")) {
l.Debug(fmt.Sprintf("octet-stream mimetype replaced with svg for %s", string(href)))
mimeType = "image/svg+xml"
}
b64 := base64.StdEncoding.EncodeToString(buf)

out := []byte(fmt.Sprintf(`<image href="data:%s;base64,%s"`, mimeType, b64))
Expand All @@ -194,7 +201,7 @@ func worker(ctx context.Context, l simplelog.Logger, inputPath string, href []by

var httpClient = &http.Client{}

func httpGet(ctx context.Context, href string) ([]byte, string, error) {
func httpGet(ctx context.Context, l simplelog.Logger, href string) ([]byte, string, error) {
ctx, cancel := context.WithTimeout(ctx, time.Minute)
defer cancel()

Expand All @@ -208,6 +215,7 @@ func httpGet(ctx context.Context, href string) ([]byte, string, error) {
return nil, "", err
}
defer resp.Body.Close()
l.Debug(fmt.Sprintf("fetched %s remotely - response code %v", string(href), resp.StatusCode))
if resp.StatusCode != 200 {
return nil, "", fmt.Errorf("expected status 200 but got %d %s", resp.StatusCode, resp.Status)
}
Expand All @@ -216,7 +224,10 @@ func httpGet(ctx context.Context, href string) ([]byte, string, error) {
if err != nil {
return nil, "", err
}
return buf, resp.Header.Get("Content-Type"), nil
contentType := resp.Header.Get("Content-Type")
l.Debug(fmt.Sprintf("fetched content type: %s, Content length: %d bytes", contentType, len(buf)))

return buf, contentType, nil
}

// sniffMimeType sniffs the mime type of href based on its file extension and contents.
Expand Down