Forum: Ruby on Rails Regarding using Web service to handle file uploads

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
87e41d0d468ad56a3b07d9a6482fd6d5?d=identicon&s=25 hemant kumar (Guest)
on 2006-04-11 00:05
(Received via mailing list)
I am trying to code a Web Service in rails that can handle file uploads.
Now
as i read, SOAP 1.1 doesn't support this yet..so i will have to use
SOAP4R,
right?

This is fine from server side...but will this API will be compatible to
.NET
clients, which will be using standard SOAP API perhaps.

Any idea, whats the way to go here?
Dd72ba9de6187137e032280b36169980?d=identicon&s=25 Kevin Marshall (falicon)
on 2006-04-11 16:34
hemant kumar wrote:
> I am trying to code a Web Service in rails that can handle file uploads.

A file upload really isn't that much different than accepting a field of
data as far as a web service is concerned...the only real consideration
is if you are accepting binary or ascii files for upload (as your
signature will change depending on the type you will accept/expect).

So basically you would just accept the data, check that it's as expected
(allowing file uploads can be a major security risk, so PLEASE make sure
you put in good security checks on what you accpet)...then write it out
to the location you want it, and send back a message to the client with
status/results...

It shouldn't really have much to do with the web service
specification...but if you need/want more details on the specific steps
to create something like this just let me know.
87e41d0d468ad56a3b07d9a6482fd6d5?d=identicon&s=25 Hemant Kumar (gnufied)
on 2006-04-11 21:36
(Received via mailing list)
Kevin thanx for the response:

I thought so....and setout to use... :base64 to handle binary file
uploads...and my code is:

require 'digest/sha1'
#require 'soap/rpc/standaloneserver'
#require 'soap/attachment'

class FileuploadApi < ActionWebService::API::Base
  api_method :put_file,
  			 :expects => [:string, :string, :base64],
  			 :returns => [:int]
end

class FileuploadService < ActionWebService::Base
  web_service_api FileuploadApi
  before_invocation :try_authenticate
  def put_file(name,password,file)
 	File.open("temp.jpg","wb") do |f|
 	  f.write(file)
 	end
  end

  def try_authenticate(name,args)
  	password = Digest::SHA1.hexdigest(args[1].strip)
  	current_user = User.find(:first,
  							 :conditions => [" name = ? and hashed_password = ?",
  							 args[0].strip,password])
	if current_user
	  return true
	else
	  return false
	 end
  end
end

first, i just want things to work...so i am not doing any
validations...but when i try to consume this web service from a Mono
app..i get:

Unhandled Exception: System.ArgumentException: Reading would overrun
buffer
in <0x001ab> System.IO.FileStream:Read (System.Byte[] dest, Int32
dest_offset, Int32 count)
in <0x00097> TestUpload.Test:Main (System.String[] args)

So, what are my options?

On 4/11/06, Kevin Marshall <info@falicon.com> wrote:
> you put in good security checks on what you accpet)...then write it out
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


--
nothing much to talk
87e41d0d468ad56a3b07d9a6482fd6d5?d=identicon&s=25 Hemant Kumar (gnufied)
on 2006-04-11 23:44
Kevin, looks like base64 only supports 8 bytes...so i make the call in a
loop...yeah its lousy method..besides i dont get any File info with
those 8 bytes...and somehow..some bits are added to the file recieved at
the server.

My modified is like:

require 'digest/sha1'
#require 'soap/rpc/standaloneserver'
#require 'soap/attachment'

class FileuploadApi < ActionWebService::API::Base
  api_method :put_file,
  			 :expects => [:string, :string, :base64],
  			 :returns => [:int]
end

class FileuploadService < ActionWebService::Base
  web_service_api FileuploadApi
  before_invocation :try_authenticate
  def put_file(name,password,data)
 	File.open("temp.png","a") do |f|
 	  f.write(data)
 	end
 	return 1
  end

  def try_authenticate(name,args)
  	password = Digest::SHA1.hexdigest(args[1].strip)
  	current_user = User.find(:first,
  							 :conditions => [" name = ? and hashed_password = ?",
  							 args[0].strip,password])
	if current_user
	  return true
	else
	  return false
	 end
  end
end

And my .NET client is:
using System;
using System.IO;

namespace TestUpload
{
  class Test
  {
    public static void Main(string[] args)
    {
      FileStream stream = new FileStream("girl.png",FileMode.Open);
      FileuploadService fileService = new FileuploadService();
      Byte[] data = new Byte[8];
      while(stream.Read(data,0,8)!=0)
        {
          int result = fileService.PutFile("hemant","hemant",data);
          if(result > 0)
            Console.WriteLine("Write Successful\n");
        }
    }
  }
}

I m really bugged by this...secure or not secure...i got to do
this..somehow...please reply you have a better solution.
This topic is locked and can not be replied to.