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

Add support for io.Writer #480

Closed
ivanjaros opened this issue Feb 7, 2024 · 3 comments
Closed

Add support for io.Writer #480

ivanjaros opened this issue Feb 7, 2024 · 3 comments

Comments

@ivanjaros
Copy link

I could really use implementation allowing me to detect mime type of incoming data, like when a file is being uploaded so that I do not have to then rewind the file and process it again with DetectReader.

So the mime writer should simply keep witting into internal buffer until some sane limit or until it can detect the mime type. After that it should be just NOOP. This could be then used with io.MultiWritter where I could write the incoming data into a file on the disk but also detect the mime type along the way since I already have access to all the bytes.

Something like:

type Writer struct {
	buff []byte
	res *MIME
}

func (w *Writer) Write(p []byte) (int64, error) {
	if w.res == nil {
		w.buff = append(w.buff, p...) // this should be buffer pool with fixed max capacity
		if result := root.match(w.buff, len(w.buff)); result != nil {
			w.res = result
			w.buff = nil // or return to pool
		}
	}
	return len(p), nil
}

func (w *Writer) Detect() *MIME {
	return w.res
}

dstW, _ := os.CreateFile("foo.txt")
mimeW := new(Writer)
w := io.MultiWriter(dstW, mimeW)
io.Copy(w, src)
@gabriel-vasile
Copy link
Owner

You can achieve something similar by using a byte buffer, io.TeeReader, and io.MultiReader.

The code would look something like this:

dstW, _ := os.CreateFile("foo.txt")
header := bytes.NewBuffer(nil)
mtype, _ := mimetype.DetectReader(io.TeeReader(src, header))

src = io.MultiReader(header, src)
// src now contains the complete, original data.

There is an example in docs.

Let me know if this solves the problem.

@ivanjaros
Copy link
Author

ivanjaros commented Feb 8, 2024

I was talking about writing, not reading. Very different situation with less options to be creative.

My main point being there should be something built-in for streams, that's all.

@gabriel-vasile
Copy link
Owner

Sorry, I don't see what's missing for streams. I think my suggestion covers your requirement of no seeks and copying the readily available data to the destination file.

In any case, a mimetype.Writer will likely not happen because it's not really writer behaviour (it does not copy some data to a destination like zip.Writer, bufio.Writer or any other writer in stdlib.)

Feel free to explain the shortcomings of current situation and I will re-open the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants