I am currently playing around with golang and Martini and such and wanted to dynamically serve some manipulated/generated images. Here's a minimal example:
package main
import (
"github.com/codegangsta/martini"
"github.com/nfnt/resize"
"image"
"image/jpeg"
"log"
"os"
)
func thumb() image.Image {
file, err := os.Open("test.jpg")
if err != nil {
log.Fatal(err)
}
img, err := jpeg.Decode(file)
if err != nil {
log.Fatal(err)
}
file.Close()
m := resize.Resize(0, 200, img, resize.MitchellNetravali)
return m
}
func main() {
m := martini.Classic()
m.Get("/") image.Image {
return thumb()
})
m.Run()
}
That compiles fine, but instead of serving an image, I get some "Content-Type:text/plain; charset=utf-8" that looks like this:
<*image.RGBA64 Value>
I am pretty sure that I need to encode the image again and then serve it. But im not quite sure how to do this without saving the image to the disk...
Thanks in advance!
You can write to the ResponseWriter directly because it implements the io.Writer interface, no need to use a buffer or copy the image to disk.
You were almost there, just needed to set the content type and like you mentioned encode the image.Image object back into a jpeg. Luckily, the jpeg.Encode() method needed a writer to write to and you have the ResponseWriter available at your disposal to do just that thanks to Martini having the ability to inject it into your handler.
Note: you will probably want to do a more robust job of error handling than I have provided. This is just to get the ball rolling. ;)
package main
import (
"image"
"image/jpeg"
"log"
"net/http"
"os"
"github.com/codegangsta/martini"
"github.com/nfnt/resize"
)
func thumb() image.Image {
file, err := os.Open("test.jpg")
if err != nil {
log.Fatal(err)
}
img, err := jpeg.Decode(file)
if err != nil {
log.Fatal(err)
}
file.Close()
m := resize.Resize(0, 200, img, resize.MitchellNetravali)
return m
}
func main() {
m := martini.Classic()
m.Get("/", func(res http.ResponseWriter, req *http.Request) {
res.Header().Set("Content-Type", "image/jpeg")
err := jpeg.Encode(res, thumb(), &jpeg.Options{100})
if err != nil {
res.WriteHeader(500)
} else {
res.WriteHeader(200)
}
})
m.Run()
}
Related
I just started learning GO and wrote this piece of code that writes an http.Response.Body to os.Stdout or to a file, but I'm not happy about the semantics of this.
I want the http.Response struct to have these receiver functions, so I can use it more easily throughout the entire app.
I know that the answers might get flagged as opinionated, but I still wonder, is there a better way of writing this?
Is there some sort of best practice?
package main
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
)
type httpResp http.Response
func main() {
res, err := http.Get("http://www.stackoverflow.com")
if err != nil {
fmt.Println("Error: ", err)
os.Exit(1)
}
defer res.Body.Close()
response := httpResp(*res)
response.toFile("stckovrflw.html")
response.toStdOut()
}
func (r httpResp) toFile(filename string) {
str, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err)
}
ioutil.WriteFile(filename, []byte(str), 0666)
}
func (r httpResp) toStdOut() {
_, err := io.Copy(os.Stdout, r.Body)
if err != nil {
panic(err)
}
}
On a side note, is there a way to make the http.Get method spit out a custom type that already has access to these receiver functions without the need for casting? So i could do something like this:
func main() {
res, err := http.Get("http://www.stackoverflow.com")
if err != nil {
fmt.Println("Error: ", err)
os.Exit(1)
}
defer res.Body.Close()
res.toFile("stckovrflw.html")
res.toStdOut()
}
Thanks!
You don't have to implement these functions. *http.Response already implements io.Writer:
Write writes r to w in the HTTP/1.x server response format, including the status line, headers, body, and optional trailer.
package main
import (
"net/http"
"os"
)
func main() {
r := &http.Response{}
r.Write(os.Stdout)
}
In the example above, the zero value prints:
HTTP/0.0 000 status code 0
Content-Length: 0
Playground: https://play.golang.org/p/2AUEAUPCA8j
In case you need additional business logic in the write methods, you can embed *http.Response in your defined type:
type RespWrapper struct {
*http.Response
}
func (w *RespWrapper) toStdOut() {
_, err := io.Copy(os.Stdout, w.Body)
if err != nil {
panic(err)
}
}
But then you must construct a variable of type RespWrapper with the *http.Response:
func main() {
// resp with a fake body
r := &http.Response{Body: io.NopCloser(strings.NewReader("foo"))}
// or r, _ := http.Get("example.com")
// construct the wrapper
wrapper := &RespWrapper{Response: r}
wrapper.toStdOut()
}
is there a way to make the http.Get method spit out a custom type
No, the return types of http.Get are (resp *http.Response, err error), that's part of the function signature, you can't change it.
I am trying to get the content of a publicly available file using ioutil.ReadFile() but it doesn't find the file: panic: open http://www.pdf995.com/samples/pdf.pdf: No such file or directory
Here's my code:
// Reading and writing files are basic tasks needed for
// many Go programs. First we'll look at some examples of
// reading files.
package main
import (
"fmt"
"io/ioutil"
)
// Reading files requires checking most calls for errors.
// This helper will streamline our error checks below.
func check(e error) {
if e != nil {
panic(e)
}
}
func main() {
fileInUrl, err := ioutil.ReadFile("http://www.pdf995.com/samples/pdf.pdf")
if err != nil {
panic(err)
}
fmt.Printf("HERE --- fileInUrl: %+v", fileInUrl)
}
Here's a go playground example
ioutil.ReadFile() does not support http.
If you look at the source code(https://golang.org/src/io/ioutil/ioutil.go?s=1503:1549#L42), open the file using os.Open.
I think I can do this coding.
package main
import (
"io"
"net/http"
"os"
)
func main() {
fileUrl := "http://www.pdf995.com/samples/pdf.pdf"
if err := DownloadFile("example.pdf", fileUrl); err != nil {
panic(err)
}
}
func DownloadFile(filepath string, url string) error {
// Get the data
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
// Create the file
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
// Write the body to file
_, err = io.Copy(out, resp.Body)
return err
}
but, go playgound not protocol(go error dial tcp: Protocol not available).
so, You have to do it PC.
Im trying to stream AES encrypted video file; however we need to decrypt it on the fly before streaming it though http
how to write the output of decrypted data to http writer
package main
import (
"crypto/aes"
"crypto/cipher"
"io"
"log"
"net/http"
"os"
)
var key []byte = []byte("yourAESPrivateKey")
func ServeHTTP(w http.ResponseWriter, r *http.Request) {
inFile, err := os.Open("2.ts")
if err != nil {
log.Fatal(err)
}
defer inFile.Close()
block, err := aes.NewCipher(key)
if err != nil {
log.Fatal(err)
return
}
var iv [aes.BlockSize]byte
stream := cipher.NewOFB(block, iv[:])
w.Header().Set("Content-type", "video/mp4")
writer := &cipher.StreamWriter{S: stream, W: w}
if _, err := io.Copy(writer, inFile); err != nil {
log.Fatal(err)
return
}
//http.ServeContent(w, r, "video.mp4", time.Now(), inFile)
}
func main() {
http.HandleFunc("/", ServeHTTP)
http.ListenAndServe(":2020", nil)
}
i have enhanced my code
so if i requested an encrypted file, the server will decrypt it and serve it properly, however i have another problem as below
the problem now is that i get corrupted file if file exceeds certain size.
i have done some tests on text files for debugging purposes and so i can post data results here (big corrupted file) check the end of the file
package main
import (
"crypto/aes"
"crypto/cipher"
"log"
"net/http"
"os"
"time"
)
type cipherHttpWriter struct {
http.ResponseWriter
}
func (c cipherHttpWriter) Write(b []byte) (int, error) {
var key []byte = []byte("we!#09bsa$.a-ala.HJOqweK45aghf&A")
block, err := aes.NewCipher(key)
if err != nil {
return 0, err
}
var iv [aes.BlockSize]byte
stream := cipher.NewOFB(block, iv[:])
streamWriter := &cipher.StreamWriter{S: stream, W: c.ResponseWriter}
defer streamWriter.Close()
return streamWriter.Write(b)
}
func ServeHTTP(w http.ResponseWriter, r *http.Request) {
inFile, err := os.Open(string(r.URL.Path[1:]))
if err != nil {
log.Fatal(err)
}
defer inFile.Close()
writer := cipherHttpWriter{}
writer.ResponseWriter = w
http.ServeContent(writer, r, "", time.Now(), inFile)
}
func main() {
http.HandleFunc("/", ServeHTTP)
http.ListenAndServe(":2020", nil)
}
I'm creating an image dynamically when one doesn't exist. IE example_t500.jpg when requested would be created from example.jpg. The problem I'm having is displaying the created image when it's requested before a missing image is shown.
Code:
package main
import (
"image/jpeg"
"net/http"
"log"
"os"
"strings"
"fmt"
"strconv"
resizer "github.com/nfnt/resize"
)
func WebHandler (w http.ResponseWriter, r *http.Request) {
var Path = "../../static/img/photos/2014/11/4/test.jpg"
ResizeImage(Path, 500)
http.Handle("/", http.FileServer(http.Dir("example_t500.jpg")))
}
func ResizeImage (Path string, Width uint) {
var ImageExtension = strings.Split(Path, ".jpg")
var ImageNum = strings.Split(ImageExtension[0], "/")
var ImageName = ImageNum[len(ImageNum)-1]
fmt.Println(ImageName)
file, err := os.Open(Path)
if err != nil {
log.Fatal(err)
}
img, err := jpeg.Decode(file)
if err != nil {
log.Fatal(err)
}
file.Close()
m := resizer.Resize(Width, 0, img, resizer.Lanczos3)
out, err := os.Create(ImageName + "_t" + strconv.Itoa(int(Width)) + ".jpg")
if err != nil {
log.Fatal(err)
}
defer out.Close()
jpeg.Encode(out, m, nil)
}
func main() {
http.HandleFunc("/", WebHandler)
http.ListenAndServe(":8080", nil)
}
This is my first time trying to use Go and am having trouble rendering the image. Any help is appreciated.
There are a few things you need to do.
First, you need to remove this line from your WebHandler function:
http.Handle("/", http.FileServer(http.Dir("example_t500.jpg")))
That's setting up the default serve mux to handle the root route - but you've already done that in your main function here:
http.HandleFunc("/", WebHandler)
So every time you hit the root route, you're effectively just telling the servemux to handle it again .. but differently.
What you want to do.. is set the Content-Type header of the response.. then copy the contents of the file to the response stream. Something like this:
func WebHandler (w http.ResponseWriter, r *http.Request) {
var Path = "../../static/img/photos/2014/11/4/test.jpg"
ResizeImage(Path, 500)
img, err := os.Open("example_t500.jpg")
if err != nil {
log.Fatal(err) // perhaps handle this nicer
}
defer img.Close()
w.Header().Set("Content-Type", "image/jpeg") // <-- set the content-type header
io.Copy(w, img)
}
Thats the manual version. There's also http.ServeFile.
EDIT:
In response to your comment - if you don't want to write the file at all and just serve it dynamically, then all you need to do is pass the http.ResponseWriter to the Encode method. jpeg.Encode takes an io.Writer .. just as io.Copy does in my original example:
// Pass in the ResponseWriter
func ResizeImage (w io.Writer, Path string, Width uint) {
var ImageExtension = strings.Split(Path, ".jpg")
var ImageNum = strings.Split(ImageExtension[0], "/")
var ImageName = ImageNum[len(ImageNum)-1]
fmt.Println(ImageName)
file, err := os.Open(Path)
if err != nil {
log.Fatal(err)
}
img, err := jpeg.Decode(file)
if err != nil {
log.Fatal(err)
}
file.Close()
m := resizer.Resize(Width, 0, img, resizer.Lanczos3)
// Don't write the file..
/*out, err := os.Create(ImageName + "_t" + strconv.Itoa(int(Width)) + ".jpg")
if err != nil {
log.Fatal(err)
}
defer out.Close()*/
jpeg.Encode(w, m, nil) // Write to the ResponseWriter
}
Accept an io.Writer in the method .. then encode it to that. You can then call your method like this:
func WebHandler (w http.ResponseWriter, r *http.Request) {
var Path = "../../static/img/photos/2014/11/4/test.jpg"
ResizeImage(w, Path, 500) // pass the ResponseWriter in
}
The function you are looking for is http.ServeFile. Use that instead of http.Handle in your code.
Very new to Go (first simple project I'm working on).
Question: How do I get an image from URL and then save it to my computer?
Here's what I have so far:
package main
import (
"fmt"
"net/http"
"image"
"io/ioutil"
)
func main() {
url := "http://i.imgur.com/m1UIjW1.jpg"
// don't worry about errors
response, _ := http.Get(url);
defer response.Body.Close()
m, _, err := image.Decode(response.Body)
error := ioutil.WriteFile("/images/asdf.jpg", m, 0644)
}
However, when I run this code, I get cannot use m (type image.Image) as type []byte in function argument
I'm assuming I have to convert image.Image (variable m) into an undefined amount of bytes? Is that the correct way to go about this?
There is no need to decode the file. Simply copy the response body to a file you've opened. Here's the deal in the modified example:
response.Body is a stream of data, and implements the Reader interface - meaning you can sequentially call Read on it, as if it was an open file.
The file I'm opening here implements the Writer interface. This is the opposite - it's a stream you can call Write on.
io.Copy "patches" a reader and a writer, consumes the reader stream and writes its contents to a Writer.
This is one of my favorite things about go - implicit interfaces. You don't have to declare you're implementing an interface, you just have to implement it to be used in some context. This allows mixing and matching of code that doesn't need to know about other code it's interacting with.
package main
import (
"fmt"
"io"
"log"
"net/http"
"os"
)
func main() {
url := "http://i.imgur.com/m1UIjW1.jpg"
// don't worry about errors
response, e := http.Get(url)
if e != nil {
log.Fatal(e)
}
defer response.Body.Close()
//open a file for writing
file, err := os.Create("/tmp/asdf.jpg")
if err != nil {
log.Fatal(err)
}
defer file.Close()
// Use io.Copy to just dump the response body to the file. This supports huge files
_, err = io.Copy(file, response.Body)
if err != nil {
log.Fatal(err)
}
fmt.Println("Success!")
}
package main
import (
"io"
"net/http"
"os"
"fmt"
)
func main() {
img, _ := os.Create("image.jpg")
defer img.Close()
resp, _ := http.Get("http://i.imgur.com/Dz2r9lk.jpg")
defer resp.Body.Close()
b, _ := io.Copy(img, resp.Body)
fmt.Println("File size: ", b)
}
Try this:
package main
import (
"fmt"
"net/http"
"net/url"
"os"
"strings"
)
var (
fileName string
fullUrlFile string
)
func main() {
fullUrlFile = "https://i.imgur.com/m1UIjW1.jpg"
r, e := http.Get(fullUrlFile)
if e != nil {
panic(e)
}
defer r.Body.Close()
buildFileName()
// Create distination
f, e := os.Create(fileName) // "m1UIjW1.jpg"
if e != nil {
panic(e)
}
defer f.Close()
// Fill distination with content
n, e := f.ReadFrom(r.Body)
if e != nil {
panic(e)
}
fmt.Println("File size: ", n)
}
func buildFileName() {
fileUrl, e := url.Parse(fullUrlFile)
if e != nil {
panic(e)
}
path := fileUrl.Path
segments := strings.Split(path, "/")
fileName = segments[len(segments)-1]
println(fileName)
}
You can also use the ReadFrom method:
package main
import (
"net/http"
"os"
)
func main() {
r, e := http.Get("https://i.imgur.com/m1UIjW1.jpg")
if e != nil {
panic(e)
}
defer r.Body.Close()
f, e := os.Create("m1UIjW1.jpg")
if e != nil {
panic(e)
}
defer f.Close()
f.ReadFrom(r.Body)
}
https://golang.org/pkg/os#File.ReadFrom
What is the type of response.Body? You should just convert that into a []byte if it is not and write that to disk. There is no reason to use the image class unless you have some reason to treat the data as an image. Just treat the data as a series of bytes and write it to the disk.