Skip to content

Commit

Permalink
fix image uploads
Browse files Browse the repository at this point in the history
  • Loading branch information
rystaf committed Feb 2, 2024
1 parent 1179fea commit b3f6a71
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
20 changes: 12 additions & 8 deletions routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,10 @@ func UserOp(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
Render(w, "index.html", state)
return
}
host := state.Host
if host == "." {
host = os.Getenv("LEMMY_DOMAIN")
}
switch r.FormValue("op") {
case "leave":
communityid, _ := strconv.ParseInt(r.FormValue("communityid"), 10, 64)
Expand Down Expand Up @@ -1053,7 +1057,7 @@ func UserOp(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
Render(w, "index.html", state)
return
}
community.Icon = lemmy.NewOptional("https://" + state.Host + "/pictrs/image/" + pres.Files[0].Filename)
community.Icon = lemmy.NewOptional("https://" + host + "/pictrs/image/" + pres.Files[0].Filename)
}
if file, handler, err := r.FormFile("banner"); err == nil {
pres, err := state.UploadImage(file, handler)
Expand All @@ -1062,7 +1066,7 @@ func UserOp(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
Render(w, "index.html", state)
return
}
community.Banner = lemmy.NewOptional("https://" + state.Host + "/pictrs/image/" + pres.Files[0].Filename)
community.Banner = lemmy.NewOptional("https://" + host + "/pictrs/image/" + pres.Files[0].Filename)
}
resp, err := state.Client.CreateCommunity(context.Background(), community)
if err == nil {
Expand Down Expand Up @@ -1094,7 +1098,7 @@ func UserOp(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
Render(w, "index.html", state)
return
}
community.Icon = lemmy.NewOptional("https://" + state.Host + "/pictrs/image/" + pres.Files[0].Filename)
community.Icon = lemmy.NewOptional("https://" + host + "/pictrs/image/" + pres.Files[0].Filename)
}
if file, handler, err := r.FormFile("banner"); err == nil {
pres, err := state.UploadImage(file, handler)
Expand All @@ -1103,7 +1107,7 @@ func UserOp(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
Render(w, "index.html", state)
return
}
community.Banner = lemmy.NewOptional("https://" + state.Host + "/pictrs/image/" + pres.Files[0].Filename)
community.Banner = lemmy.NewOptional("https://" + host + "/pictrs/image/" + pres.Files[0].Filename)
}
resp, err := state.Client.EditCommunity(context.Background(), community)
if err == nil {
Expand Down Expand Up @@ -1138,7 +1142,7 @@ func UserOp(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
Render(w, "index.html", state)
return
}
post.URL = lemmy.NewOptional("https://" + state.Host + "/pictrs/image/" + pres.Files[0].Filename)
post.URL = lemmy.NewOptional("https://" + host + "/pictrs/image/" + pres.Files[0].Filename)
}
if r.FormValue("body") != "" {
post.Body = lemmy.NewOptional(r.FormValue("body"))
Expand Down Expand Up @@ -1179,7 +1183,7 @@ func UserOp(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
Render(w, "index.html", state)
return
}
post.URL = lemmy.NewOptional("https://" + state.Host + "/pictrs/image/" + pres.Files[0].Filename)
post.URL = lemmy.NewOptional("https://" + host + "/pictrs/image/" + pres.Files[0].Filename)
}

resp, err := state.Client.EditPost(context.Background(), post)
Expand Down Expand Up @@ -1347,7 +1351,7 @@ func UserOp(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
Render(w, "index.html", state)
return
}
content += ("![](https://" + state.Host + "/pictrs/image/" + pres.Files[0].Filename + ")")
content += ("![](https://" + host + "/pictrs/image/" + pres.Files[0].Filename + ")")
}
if r.FormValue("submit") == "save" {
createComment := lemmy.CreateComment{
Expand Down Expand Up @@ -1408,7 +1412,7 @@ func UserOp(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
Render(w, "index.html", state)
return
}
content += ("![](https://" + state.Host + "/pictrs/image/" + pres.Files[0].Filename + ")")
content += ("![](https://" + host + "/pictrs/image/" + pres.Files[0].Filename + ")")
}

if r.FormValue("submit") == "save" {
Expand Down
6 changes: 5 additions & 1 deletion state.go
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,11 @@ func (state *State) UploadImage(file multipart.File, header *multipart.FileHeade
}
io.Copy(part, file)
writer.Close()
req, err := http.NewRequest("POST", "https://"+state.Host+"/pictrs/image", body)
host := state.Host
if host == "." {
host = os.Getenv("LEMMY_DOMAIN")
}
req, err := http.NewRequest("POST", "https://"+host+"/pictrs/image", body)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit b3f6a71

Please sign in to comment.