Git Product home page Git Product logo

Comments (4)

h2non avatar h2non commented on May 23, 2024

file reader stream is stateful and when the stream is consumed, data will be drained. The simplest way to solve this is to reinitialize the reader by opening the file again or, alternatively, writing head data back into the reader buffer via file.WriteAt(head, 0)

from filetype.

helloshaohua avatar helloshaohua commented on May 23, 2024

@h2non I tried to write it, but it didn't work

from filetype.

h2non avatar h2non commented on May 23, 2024

You could just close the first file reader stream, then open it again and read the full content.

from filetype.

sergolius avatar sergolius commented on May 23, 2024

@helloshaohua
There are two options:

  1. As your case is a file you can use Seek method _, err := file.Seek(0, io.SeekStart). Next read start from zero;
  2. For any case is to use []byte buffer, like you did, but also chain readers with io.MultiReader
file, err := os.Open("../../test/assets/HgWFAEPozfVdcst")
// ...
head := make([]byte, 262)
_, _ = io.ReadFull(file, head)
match, err := filetype.Match(head)
// ...
reader := io.MultiReader(bytes.NewBuffer(head), file)
// ...
err = o.client.PutObject("HgWFAEPozfVdcst", reader) // put io.MultiReader

p.s. Don't forget to close the file/reader

from filetype.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.