Search for a word in a file before uploading

Hello!

I am using this example for file uploader.

Before uploading a file I would like to open it, search for a specific word, assign this word to an attribute in the database and only then save the file in the database.

Example:

I select two files and click “upload”

The file name is assigned to a upload_file_name. File’s size is assigned to an upload_file_size.

File is opened, I search for a word “Bug” or “Lion”. If i find “Lion”, “Lion” should be assigned to an upload_content_type. If I find “Bug”, “Bug” should be assigned to upload_content_type.

I am not quite sure where I have to define the function that will open a file: in uploads_controller.rb or in uploads.rb? And I do not know how to assign a “Bug” to an upload_content_type.

That is my upload.rb:

class Upload < ActiveRecord::Base

attr_accessible :upload, :upload_file_name, :upload_file_size

Paperclip::interpolates :piks do |attachment, style|

attachment.instance.upload_file_name

end

has_attached_file :upload,

:url =>“/system/Files/Files/:piks/:basename.:extension”,

:path =>“:rails_root/public/system/Files/Files/:piks/:basename.:extension”

include Rails.application.routes.url_helpers

validates :upload_file_name, :presence => true,

:format =>{:with => %r{.(txt)$}i,:message =>“should have an extension .cell”}

validates_uniqueness_of :upload_file_name, :message =>“exists already.”

def to_jq_upload

{

“name” => (read_attribute(:upload_file_name)).split(“.”).first,

“size” => read_attribute(:upload_file_size),

“url” => upload.url(:original),

“delete_url” => upload_path(self),

“delete_type” => “DELETE”

}

my uploads_controller.rb:

def create

p_attr=params[:upload]

p_attr[:upload] = params[:upload][:upload].first if params[:upload][:upload].class == Array

@upload = Upload.new(p_attr)

respond_to do |format|

if @upload.save

format.html {

render :json => [@upload.to_jq_upload].to_json,

:content_type => ‘BUUUUU’,

:layout => false

}

format.json { render json: [@upload.to_jq_upload].to_json, status: :created, location: @upload }

else

format.html { render action: “new” }

format.json{ render json: {name:(@upload.upload_file_name).split(“.”).first ,error: @upload.errors.messages[:upload_file_name]}, :status =>422}

end

end

end

Database:

ActiveRecord::Schema.define(:version => 20120731045929) do

create_table “uploads”, :force => true do |t|

t.string “upload_file_name”

t.string “upload_content_type”

t.string “user”

t.integer “upload_file_size”

t.datetime “upload_updated_at”

t.datetime “created_at”, :null => false

t.datetime “updated_at”, :null => false

end

end

And function to open a file:

def check

File.open(??filename) do |file|

  file.each do |line|
	 type=/Lion/.match(line)
	 if type != nil
  	puts(type[0])   #assign to a database!!
  	break
	 end
  end

end

end

Thanks in advance

All rb files are executed in the server. If you want to check the file *before* uploading it you will have to do it in the client pc, presumably in javascript. I don't know whether this is even possible, I doubt it. Do you think it would be a good thing if a web page could look inside files on your pc? I think you will have to upload it and then do the work.

Are you a beginner at rails? If so then I suggest you work through a good tutorial such as railstutorial.org (which is free to use online).

Colin

All rb files are executed in the server. If you want to check the file before uploading it you will have to do it in the client pc, presumably in javascript. I don’t know whether this is even possible, I doubt it. Do you think it would be a good thing if a web page could look inside files on your pc? I think you will have to upload it and then do the work.

Are you a beginner at rails? If so then I suggest you work through a good tutorial such as railstutorial.org (which is free to use online).

Colin

All rb files are executed in the server. Yeas, but when you try to upload a file that already exists in the database, it does not let you do so. So, it is possible to validate a file, namely a file name or extension or size before uploading the file. Is it possible to do the same thing with the “word finding”? Or it is two different things? And I have looked through that tutorial already. Thank you=)

Katja

And there is a preview of the files, cannot I make use of it somehow?

What makes you think the check is performed before the upload?

The check is performed during the upload. If the filename exists already in the database it will not be uploaded twice. Cannot I use this “during uploading” time to check the content of the file?

The check is performed during the upload. If the filename exists already in the database it will not be uploaded twice. Cannot I use this "during uploading" time to check the content of the file?

The upload operation is the act of transmitting the file from the client PC to the server. Actions may be performed before the file is uploaded (at the client) or after the file is uploaded (at the server, operations such as updating the database). Nothing other than the transfer of the file is going to happen "during uploading". It may be that you want to do the things you want after the file is transferred but before the database is updated.

Colin

That does not mean the file was not sent to the server.

I phrased my question poorly, so here it is again, hopefully more clear: what makes you think the check is performed before the form (containing the file contents along with the other form data) is posted to the server?