AyKarsi
AyKarsi

Reputation: 9675

TagLib-sharp: Reading metadata from HttpPostedFile object

User post their MP3s to my site and I would like to read the metadata from the files before they are stored in the CDN. TagLib-Sharp seems to be library to go for this, but I can't see any way to open a HttPostedFile, which I don't not want to save to disk, and retrieve the metadata.

Anybody have an example on how to do this with taglib-sharp?

Edit: It seems that IFileAbstraction can solve this. Anybody know how to use IFileAbstraction?

Upvotes: 7

Views: 1576

Answers (1)

Brian Nickel
Brian Nickel

Reputation: 27550

You would want to do something as follows. The caveat is that the steam has to be seekable an I do not know if HttpPostedFile.InputStream is.

TagLib.File myFile = TagLib.File.Create(new HttpPostedFileAbstraction(postedFile));

public class HttpPostedFileAbstraction : TagLib.File.IFileAbstraction
{
    private HttpPostedFile file;

    public HttpPostedFileAbstraction(HttpPostedFile file)
    {
        this.file = file;
    }

    public string Name {
        get { return file.FileName; }
    }

    public System.IO.Stream ReadStream {
        get { return file.InputStream; }
    }

    public System.IO.Stream WriteStream {
        get { throw new Exception("Cannot write to HttpPostedFile"); }
    }

    public void CloseStream (System.IO.Stream stream) { }
}

Upvotes: 6

Related Questions