libCURL Multipart POST with a large file - post

I need to upload a large file (>2 GB) using multipart POST request. Source file can be named using unicode symbols. The problem is that libcurl does not support unicode wfopen in windows, so I am not able to complete this task in usual way like
curl_formadd(&formpost, &lastptr,
CURLFORM_COPYNAME, fieldname,
CURLFORM_FILENAME, filename,
CURLFORM_FILE, full_path_to_file,
CURLFORM_CONTENTTYPE, "application/octet-stream",
CURLFORM_END);
I figured out that I can use a CURLFORM_STREAM option of curl_formadd in conjunction with CURLOPT_READFUNCTION. Now I need to manually set the file size through CURLFORM_CONTENTSLENGTH option, but it accepts only "long" as a parameter when I need to set a "long long" file size. After a look through curl manual I found some CURLOPT_POSTFIELDSIZE_LARGE option, but it does nothing in my case. It seems that multipart request system ignores this parameter. I don't know what to do, I don't want to give up unicode names or large files support.

Related

Is there a way to check if a Ruby variable contains binary data?

I'm using Ruby 2.4 and Rails 5. I have file content in a variabe named "content". The content could contain data from things like a PDF file, a Word file, or an HTML file. Is there any way to tell if the variable contains binary data? Ultimately, I would like to know if this is a PDf, Microsoft Office, or some other type of OpenOffice file. This answer -- Rails: possible to check if a string is binary? -- suggests that I can check the encoding of the variable
content.encoding
and it would produce
ASCII-8BIT
in the case of binary data, however, I've noticed there are cases where HTML content stored in the variable could also return "ASCII-8BIT" as the content.encoding, so using "content.encoding" is not a foolproof way to tell me if I have binary data. Does such a way exist and if so, what is it?
If your real question is not about binary data per se but about determining the file type of the data, I'd recommend to have a look at the ruby-filemagic gem which will give you this information much more reliably. The gem is a simple wrapper around the libmagic library which is standard on unix-like systems. The library works by scanning the content of a file and matching it against a set of known "magic" patterns in various file types.
Sample usage for a string buffer (e.g. data read form the database):
require "ruby-filemagic"
content = File.read("/.../sample.pdf") # just an example to get some data
fm = FileMagic.new
fm.buffer(content)
#=> "PDF document, version 1.4"
For the gem to work (and compile) you need the file utility as well as the magic library with headers installed on your system. Quoting from the readme:
The file(1) library and headers are required:
Debian/Ubuntu:: +libmagic-dev+
Fedora/SuSE:: +file-devel+
Gentoo:: +sys-libs/libmagic+
OS X:: brew install libmagic
Tested to work well under Rails 5.
If you're on an unix machine, you can use the file command:
file titi.pdf
You could then do something like:
require 'open2'
cmd = 'file -'
Open3.popen3(cmd) do |stdin, stdout, wait_thr|
stdin.write(content)
stdin.close
puts "file type is:" + stoud.read
end

how to overwrite, or delete the file, used by writefile() calls?

I use the following to save screen output to a file
writefile("file.txt"),
tex(expression),
closefile()
The above sends the output of the tex() to the file automatically. which is all and well and what I want. (side-point: It also sends an annoying NIL line each time to the file, which I had to parse put later).
Now, when running the above code again, the file is appended to, which is not what I want. I want to either overwrite the file each time, or if there is a way to delete the file, so I can call delete on it before.
I looked at help and not able to find a command to delete a file, and I also see no option to tell writefile() to overwrite the file?
Is there an option or way around this? I am on windows 7, Maxima version: 5.36.1
Lisp: SBCL 1.2.7
I guess you are trying to capture the output of tex into a file. If so, here are a couple of other ways to do it:
tex (expr, destination);
where destination is either a file name (which is appended) or a stream, as created by opena or openw and closed by close. By the way, destination could be false, in which case tex returns a string.
with_stdout (destination, tex (expr));
where again destination is either a file name (which is appended or clobbered, as determined by the global flag file_output_append) or a stream.
with_stdout could be useful if you want to mix in some output not generated by tex, e.g., print("% some commentary");.

Delphi : how to check if a file exists (path over 255 characters)

I need to make my delphi app able to check if a file copied using Robocopy is there or not when its path exceeds 255 characters.
I have tried the usual "If FileExists(MyFile) then ... " but it always returns "false" even if the file is there.
I also tried to get the file's date but I get 1899/12/30 which can be considered as an empty date.
A File search does not return anything either.
Prefix the file name with \\?\ to enable extended-length path parsing. For example you would write
if FileExists('\\?\'+FileName) then
....
Note that this will only work if you are calling the Unicode versions of the Win32 API functions. So if you use a Unicode Delphi then this will do the job. Otherwise you'll have to roll your own version of FileExists that calls Unicode versions of the API functions.
These issues are discussed in great length over on MSDN: Naming Files, Paths, and Namespaces.

Rails: possible to check if a string is binary?

In a particular Rails application, I'm pulling binary data out of LDAP into a variable for processing. Is there a way to check if the variable contains binary data? I don't want to continue with processing of this variable if it's not binary. I would expect to use is_a?...
In fact, the binary data I'm pulling from LDAP is a photo. So maybe there's an even better way to ensure the variable contains binary JPEG data? The result of this check will determine whether to continue processing the JPEG data, or to render a default JPEG from disk instead.
There is actually a lot more to this question than you might think. Only since Ruby 1.9 has there been a concept of characters (in some encoding) versus raw bytes. So in Ruby 1.9 you might be able to get away with requesting the encoding. Since you are getting stuff from LDAP the encoding for the strings coming in should be well known, most likely ISO-8859-1 or UTF-8.
In which case you can get the encoding and act on that:
some_variable.encoding # => when ASCII-8BIT, treat as a photo
Since you really want to verify that the binary data is a photo, it would make sense to run it through an image library. RMagick comes to mind. The documentation will show you how to verify that any binary data is actually JPEG encoded. You will then also be able to store other properties such as width and height.
If you don't have RMagick installed, an alternative approach would be to save the data into a Tempfile, drop down into Unix (assuming you are on Unix) and try to identify the file. If your system has ImageMagick installed, the identify command will tell you all about images. But just calling file on it will tell you this too:
~/Pictures$ file P1020359.jpg
P1020359.jpg: JPEG image data, EXIF standard, comment: "AppleMark"
You need to call the identify and file commands in a shell from Ruby:
%x(identify #{tempfile})
%x(file #{tempfile})

How do I produce a .SRM file containing string resources?

The example I am looking at is in the TurboPower FlashFiler database.
It has, for example, a file ffclcnst.rc which contains
FF_CLIENT_STRINGS RCDATA FFCLCNST.SRM
and I can run
BRCC32 ffclcnst.rc
at the command prompt which seems to compile the .SRM file into a .RES file, but I cannot see how to change the information in the .SRM file. It appears to come from the ffclcnst.str file so I assume there is some way to convert the .STR file into the .SRM file.
The readme tells you:
Most of FlashFiler's error messages are stored in string resource
files having the extension STR. If you change these files, you must
recompile them using the TurboPower String Resource Manager located at
http://sourceforge.net/projects/tpsrmgr
Please note that these are not standard Windows string resources. They're something TurboPower made up. For a full explanation, be sure to read the String Resource Manager's documentation. You can make an ordinary string-table resource in your .rc file, or you can skip the .rc file altogether and declare resourcestring constants directly in your Delphi code. Unless you're editing TurboPower code, or you need to support ancient Delphi versions, I recommend you just use normal string tables.

Resources