Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 299
AyKarsi Avatar asked Nov 12 '11 17:11

AyKarsi


1 Answers

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) { }
}
like image 150
Brian Nickel Avatar answered Oct 28 '22 14:10

Brian Nickel