在Go语言中正确使用os.NewFile的方法

huangapple go评论103阅读模式
英文:

Correct usage of os.NewFile in Go

问题

我试图在内存中组合一张图像,并通过http.ResponseWriter发送出去,而不接触文件系统。

我使用以下代码创建一个新文件:

file := os.NewFile(0, "temp_destination.png")

然而,我似乎无法对这个文件做任何操作。这是我正在使用的函数(在http.HandleFunc中调用,它只是将文件的字节发送到浏览器),它的目的是在临时文件上绘制一个蓝色矩形并将其编码为PNG:

func ComposeImage() ([]byte) {
    img := image.NewRGBA(image.Rect(0, 0, 640, 480))
    blue := color.RGBA{0, 0, 255, 255}
    draw.Draw(img, img.Bounds(), &image.Uniform{blue}, image.ZP, draw.Src)

    // 在内存中创建目标文件,而不是写入文件系统
    file := os.NewFile(0, "temp_destination.png")

    // 将图像写入目标io.Writer
    png.Encode(file, img)

    bytes, err := ioutil.ReadAll(file)
    if err != nil {
        log.Fatal("无法将临时文件读取为字节。")
    }

    return bytes
}

如果我删除png.Encode调用,只返回文件字节,服务器将永远挂起并且不执行任何操作。

保留png.Encode调用会导致文件字节(编码的,包括一些我期望看到的PNG块)被输出到stderr/stdout(我无法确定哪个),并且服务器无限期地挂起。

我猜我只是没有正确使用os.NewFile。有人可以指点我正确的方向吗?欢迎提供关于如何正确执行内存中文件操作的替代建议。

英文:

I'm attempting to compose an image in memory and send it out through http.ResponseWriter without ever touching the file system.

I use the following to create a new file:

file := os.NewFile(0, "temp_destination.png")

However, I don't seem to be able to do anything at all with this file. Here is the function I'm using (which is being called within an http.HandleFunc, which just sends the file's bytes to the browser), which is intended to draw a blue rectangle on a temporary file and encode it as a PNG:

func ComposeImage() ([]byte) {
	img := image.NewRGBA(image.Rect(0, 0, 640, 480))
	blue := color.RGBA{0, 0, 255, 255}
	draw.Draw(img, img.Bounds(), &image.Uniform{blue}, image.ZP, draw.Src)

	// in memory destination file, instead of going to the file sys
	file := os.NewFile(0, "temp_destination.png")

	// write the image to the destination io.Writer
	png.Encode(file, img)

	bytes, err := ioutil.ReadAll(file)
	if err != nil {
		log.Fatal("Couldn't read temporary file as bytes.")
	}

	return bytes
}

If I remove the png.Encode call, and just return the file bytes, the server just hangs and does nothing forever.

Leaving the png.Encode call in results in the file bytes (encoded, includes some of the PNG chunks I'd expect to see) being vomited out to stderr/stdout (I can't tell which) and server hanging indefinitely.

I assume I'm just not using os.NewFile correctly. Can anyone point me in the right direction? Alternative suggestions on how to properly perform in-memory file manipulations are welcome.

答案1

得分: 7

os.NewFile是一个低级函数,大多数人不会直接使用它。它接受一个已经存在的文件描述符(文件的系统表示)并将其转换为*os.File(Go语言的表示)。

如果你不希望图片接触到你的文件系统,完全不要使用os包。只需将你的ResponseWriter视为io.Writer,并将其传递给png.Encode

png.Encode(yourResponseWriter, img)

如果你坚持要写入一个“内存文件”,我建议使用bytes.Buffer:

buf := new(bytes.Buffer)
png.Encode(buf, img)
return buf.Bytes()
英文:

os.NewFile is a low level function that most people will never use directly. It takes an already existing file descriptor (system representation of a file) and converts it to an *os.File (Go's representation).

If you never want the picture to touch your filesystem, stay out of the os package entirely. Just treat your ResponseWriter as an io.Writer and pass it to png.Encode.

png.Encode(yourResponseWriter, img)

If you insist on writing to an "in memory file", I suggest using bytes.Buffer:

buf := new(bytes.Buffer)
png.Encode(buf, img)
return buf.Bytes()

答案2

得分: 5

请仔细阅读NewFile文档。NewFile并不会创建一个新文件,一点也不!它会设置一个围绕给定文件描述符(在你的情况下是0,我认为是stdin)的Go os.File。

无需文件即可提供图像服务更加简单:只需将图像编码到ResponseWriter中。这就是接口的作用。无需写入某个神奇的“内存文件”,也无需使用ReadAll将其读回来,简单明了:将内容写入响应中。

英文:

Please have a detailed read of the NewFile documentation. NewFile does not create a new file, not at all! It sets up a Go os.File which wraps around an existing file with the given file descriptor (0 in your case which is stdin I think).

Serving images without files is much easier: Just Encode your image to your ResponseWriter. That's what interfaces are there for. No need to write to ome magic "in memory file", no need to read it back with ReadAll, plain and simple: Write to your response.

huangapple
  • 本文由 发表于 2013年7月12日 08:07:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/17605588.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定