fix image uploads

This commit is contained in:
Ryan Stafford 2024-02-01 22:54:22 -05:00
parent 1179fea81b
commit b3f6a7149d
2 changed files with 17 additions and 9 deletions

View file

@ -966,6 +966,10 @@ func UserOp(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
Render(w, "index.html", state) Render(w, "index.html", state)
return return
} }
host := state.Host
if host == "." {
host = os.Getenv("LEMMY_DOMAIN")
}
switch r.FormValue("op") { switch r.FormValue("op") {
case "leave": case "leave":
communityid, _ := strconv.ParseInt(r.FormValue("communityid"), 10, 64) communityid, _ := strconv.ParseInt(r.FormValue("communityid"), 10, 64)
@ -1053,7 +1057,7 @@ func UserOp(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
Render(w, "index.html", state) Render(w, "index.html", state)
return 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 { if file, handler, err := r.FormFile("banner"); err == nil {
pres, err := state.UploadImage(file, handler) pres, err := state.UploadImage(file, handler)
@ -1062,7 +1066,7 @@ func UserOp(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
Render(w, "index.html", state) Render(w, "index.html", state)
return 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) resp, err := state.Client.CreateCommunity(context.Background(), community)
if err == nil { if err == nil {
@ -1094,7 +1098,7 @@ func UserOp(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
Render(w, "index.html", state) Render(w, "index.html", state)
return 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 { if file, handler, err := r.FormFile("banner"); err == nil {
pres, err := state.UploadImage(file, handler) pres, err := state.UploadImage(file, handler)
@ -1103,7 +1107,7 @@ func UserOp(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
Render(w, "index.html", state) Render(w, "index.html", state)
return 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) resp, err := state.Client.EditCommunity(context.Background(), community)
if err == nil { if err == nil {
@ -1138,7 +1142,7 @@ func UserOp(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
Render(w, "index.html", state) Render(w, "index.html", state)
return 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") != "" { if r.FormValue("body") != "" {
post.Body = lemmy.NewOptional(r.FormValue("body")) post.Body = lemmy.NewOptional(r.FormValue("body"))
@ -1179,7 +1183,7 @@ func UserOp(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
Render(w, "index.html", state) Render(w, "index.html", state)
return 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) resp, err := state.Client.EditPost(context.Background(), post)
@ -1347,7 +1351,7 @@ func UserOp(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
Render(w, "index.html", state) Render(w, "index.html", state)
return 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" { if r.FormValue("submit") == "save" {
createComment := lemmy.CreateComment{ createComment := lemmy.CreateComment{
@ -1408,7 +1412,7 @@ func UserOp(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
Render(w, "index.html", state) Render(w, "index.html", state)
return 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" { if r.FormValue("submit") == "save" {

View file

@ -683,7 +683,11 @@ func (state *State) UploadImage(file multipart.File, header *multipart.FileHeade
} }
io.Copy(part, file) io.Copy(part, file)
writer.Close() 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 { if err != nil {
return nil, err return nil, err
} }