Audio files always null using Taglib

450 views Asked by At

I'm trying to build an app in Rails which will take audio file uploads and read metadata off them to populate a database. I'm using the Taglib-ruby gem to handle various file types. The uploads seem to be working on their own, but Taglib considers any file given to it as null.

Here's my controller:

class UploadsController < ApplicationController
    require 'taglib'

    def new
    end

    def create
        file = params[:upload]
        TagLib::FileRef.open(file) do |fileref|
            unless fileref.null?
                tag = fileref.tag
                # properties = fileref.audio_properties
                @song = Song.new(title: tag.title, artist: tag.artist, album: tag.album,
                    year: tag.year, track: tag.track, genre: tag.genre)
                if @song.save
                    redirect_to songs_path
                else
                    render 'new'
                end
            else
                raise "file was null"
            end
        end
    end
end

and my view for form submission:

<h1> Upload </h1>

<%= form_tag(url: { action: :create }, html: { multipart: true }) do %>

    <%= label_tag :upload, "Scan your song:" %>
    <%= file_field_tag :upload, multiple: true %>

    <br />
    <%= submit_tag "Submit" %>

<% end %>

Taglib itself seems to be working - adding "require 'taglib'" removed the error I had been getting in regards to that, and a mock-up I made of this outside of rails worked fine (so the files I'm using are also not the problem). Every time I run this, the control flow hits my raise command, and no record is saved. It's clear that fileref.null? is returning true, which suggests to me that there's something wrong with the upload process... but I'm not sure what.

Ideally, I'd like to use the multiple uploads option and run this process on each file sequentially, but I can't even get a single upload to register as anything but null.

0

There are 0 answers