How to check that my params['Filedata'] is corrupted or not?
I have function it's reading file from params['Filedata'] and writing it to the other file.
File.open(upload_file, "wb") { |f| f.write(params['Filedata'].read) }
this line working fine for me..
But when i am calling this function with delayed job function send_later than I am getting error with params['Filedata'].read.
I'm guessing params['Filedata'] refers to a uploaded file.
Rails handles uploaded data as temporary files, which are deleted after the request is completed. If you want to access the file later, you need to copy it to a more permanent location.
Related
I've written a small FilePond example (excellent piece of work BTW) but there is one thing that I can't understand:
When I upload, e.g., "foo.JPG" or "foo.jpeg" it is saved as "foo.jpg" on the server.
I would like to keep the file extension, but don't know how to do it.
I've tried the FileRename-plugin but the files are still saved as .jpg.
I worked out a solution. If anyone is interested I can post complete code, but basically I stored the real filename (e.g., "foo.jpeg") in metadata and extracted it on the PHP server side, and then renamed the temporary file using that name instead.
In FilePond.create:
beforeAddFile: (fileItem) => new Promise(resolve => {
fileItem.setMetadata('true_filename', fileItem.filename);
resolve(true);
}),
I have a script that's a plugin for redmine which enhances the application to send encrypted mail using gpg. At some point this stopped working. Unfortunately the one who wrote that script is not available anymore and I am an admin with only very limited knowledge of RoR.
The problem is, that obviously the script creates a file with the mail body, saves it to temp, encrypts it to an output file, reads this output and then sends the mail.
With an empty /tmp directory (such as after rebooting the whole server), the gpg.in file gets created when I try to send a test mail. But then I get an error that the gpg.out file was not available. Creating it using touch does cause an empty email being send so obviously the script does not write anything to that file.
File.open('/tmp/gpg.in', 'w') do |f1| #<--- Works, file is created
f1.puts(body)
end
list_of_keys = [ rec ]
Gpgr::Encrypt.file('/tmp/gpg.in', :to => '/tmp/gpg.out').encrypt_using(list_of_keys) #<- gpg.out wird nicht erzeugt.
text = ""
File.open('/tmp/gpg.out', 'r') do |f2| #<- throws file not found error, if file not there. When file was created empty using touch, it sends an empty mail
With my limited RoR knowledge, I can't figure out how to debug this. Permissions on /tmp are 777 so the script should be allowed to write there and obviously has because File.open('/tmp/gpg.in', 'w') works correctly all the time without an error. Hence I expect the problem in Gpgr::Encrypt.file not working correctly, but I also don't get any error from that function it fails silently.
I'm working on a Lua script which will be hosted by a third party program (some .exe which will call a certain function in my script). In order to implement a functionality I need (make a rest call to a webservice to retrieve certain info) I want to use socket.http.request.
I've first build an example script for the call I wanted to make:
local io = require("io")
local http = require("socket.http")
local ltn12 = require("ltn12")
local data = "some data")
local response = {}
socket.http.request({
method = "POST",
url = "http://localhost:8080/someServce/rest/commands/someCommand",
headers = {
["Content-Type"] = "application/x-www-form-urlencoded",
["Content-Length"] = string.len(data)
},
source = ltn12.source.string(data),
sink = ltn12.sink.table(response)
})
print(table.concat(response))
print("Done")
This works fine. I get the response I expect.
Now when I try to do this from the third party host, I first got an error:
module 'socket.http' not found:
no field package.preload['socket.http']
no file '.\socket\http.lua'
no file 'D:\SomeFolder\lua\socket\http.lua'
no file 'D:\SomeFolder\lua\socket\http\init.lua'
no file 'D:\SomeFolder\socket\http.lua'
no file 'D:\SomeFolder\socket\http\init.lua'
no file 'C:\Program Files (x86)\Lua\5.1\lua\socket\http.luac'
no file '.\socket\http.dll'
no file 'D:\SomeFolder\socket\http.dll'
no file 'D:\SomeFolder\loadall.dll'
no file '.\socket.dll'
no file 'D:\SomeFolder\socket.dll'
no file 'D:\SomeFolder\loadall.dll'
I've tried copying the socket folder from the LUA folder to the folder the host is executing from (D:\SomeFolder). It then finds the module, but fails to load it with another error:
loop or previous error loading module 'socket.http'
I've also tried moving the require statement outside of the function and making it global. This gives me yet another error:
module 'socket.core' not found:
no field package.preload['socket.core']
no file '.\socket\core.lua'
no file 'D:\SomeFolder\lua\socket\core.lua'
no file 'D:\SomeFolder\lua\socket\core\init.lua'
no file 'D:\SomeFolder\socket\core.lua'
no file 'D:\SomeFolder\socket\core\init.lua'
no file 'C:\Program Files (x86)\Lua\5.1\lua\socket\core.luac'
no file 'C:\Program Files (x86)\Lua\5.1\lua\socket\core.lua'
no file '.\socket\core.dll'
no file 'D:\SomeFolder\socket\core.dll'
no file 'D:\SomeFolder\loadall.dll'
no file '.\socket.dll'
no file 'D:\SomeFolder\socket.dll'
no file 'D:\SomeFolder\loadall.dll'
Then I tried copying the core.dll from socket into the D:\SomeFolder folder and it gave me another error:
error loading module 'socket.core' from file '.\socket\core.dll':
%1 is not a valid Win32 application.
Now I'm stuck. I think I must be doing something completely wrong, but I can't find any proper description on how to fix issues like this. Can anyone help me out?
As it turns out, the actual path Lua is going to look for is the problem here. Together with the third party we found that if we put a set of libraries in D:\SomeFolder\ everything now works. So for example there is now a socket.lua in D:\SomeFolder\and there are a socket and a mime forlder there as well.
Rule of thumb appears to be that the location of lua5.1.dll that is bound by the application is leading for the location of any modules you want to load.
You probably need to have the following folder structure (relative to your D:\SomeFolder folder):
socket.lua
socket/core.dll
socket/http.lua
socket/url.lua
socket/<any other file from socket folder required by http.lua>
I just tested this configuration and it works for me.
loop or previous error loading module 'socket.http'
This is usually caused by loading socket.http from socket/http.lua file itself.
I am trying to use
TZipFile.ZipDirectoryContents()
Like so:
TZipFile.ZipDirectoryContents('Test.PCF', WorkingDir);
If I am reading this right, it should save the contents of folder "workingdir" into a file named Test.pcf.
Now when I do this I get error::
Raised exception class EFOpenError with message Cannot open file
...test.pcf. The process cannot access the file because its being used by another process."
Two things confuse me:
It says that it cannot open file. There is no test.pcf yet. I was hoping this would create it.
It says cannot access file. Is this because it's not created yet? Am I using this function wong? If so how would I create a zip file from a folder location?
I tested your code and it failed in the same way as you reported.
I then created an empty zip file manually by running WinZip.
Then ran your code and it ran fine.
It appears that the zip file has to already exist for ZipDirectoryContents to work.
To create a zip file programatically:
myZipFile := TZIpFile.Create;
myZipFile.Open('c:\myfolder\newzipfile.zip', TZipMode.zmWrite);
myZipFile.Close;
myZipFile.Free;
This line will then work:
TZipFile.ZipDirectoryContents('c:\myfolder\newzipfile.zip', WorkingDir);
I'm having some problems reading a file from S3. I want to be able to load the ID3 tags remotely, but using open-URI doesn't work, it gives me the following error:
ruby-1.8.7-p302 > c=TagLib2::File.new(open(URI.parse("http://recordtemple.com.s3.amazonaws.com/music/745/original/The%20Stranger.mp3?1292096514")))
TypeError: can't convert Tempfile into String
from (irb):8:in `initialize'
from (irb):8:in `new'
from (irb):8
However, if i download the same file and put it on my desktop (ie no need for open-URI), it works just fine.
c=TagLib2::File.new("/Users/momofwombie/Desktop/blah.mp3")
is there something else I should be doing to read a remote file?
UPDATE: I just found this link, which may explain a little bit, but surely there must be some way to do this...
Read header data from files on remote server
Might want to check out AWS::S3, a Ruby Library for Amazon's Simple Storage Service
Do an AWS::S3:S3Object.find for the file and then an use about to retrieve the metadata
This solution assumes you have the AWS credentials and permission to access the S3 bucket that contains the files in question.
TagLib2::File.new doesn't take a file handle, which is what you are passing to it when you use open without a read.
Add on read and you'll get the contents of the URL, but TagLib2::File doesn't know what to do with that either, so you are forced to read the contents of the URL, and save it.
I also noticed you are unnecessarily complicating your use of OpenURI. You don't have to parse the URL using URI before passing it to open. Just pass the URL string.
require 'open-uri'
fname = File.basename($0) << '.' << $$.to_s
File.open(fname, 'wb') do |fo|
fo.print open("http://recordtemple.com.s3.amazonaws.com/music/745/original/The%20Stranger.mp3?1292096514").read
end
c = TagLib2::File.new(fname)
# do more processing...
File.delete(fname)
I don't have TagLib2 installed but I ran the rest of the code and the mp3 file downloaded to my disk and is playable. The File.delete would clean up afterwards, which should put you in the state you want to be in.
This solution isn't going to work much longer. Paperclip > 3.0.0 has removed to_file. I'm using S3 & Heroku. What I ended up doing was copying the file to a temporary location and parsing it from there. Here is my code:
dest = Tempfile.new(upload.spreadsheet_file_name)
dest.binmode
upload.spreadsheet.copy_to_local_file(:default_style, dest.path)
file_loc = dest.path
...
CSV.foreach(file_loc, :headers => true, :skip_blanks => true) do |row|}
This seems to work instead of open-URI:
Mp3Info.open(mp3.to_file.path) do |mp3info|
puts mp3info.tag.artist
end
Paperclip has a to_file method that downloads the file from S3.