Is it possible to load a [String : Any] dictionary into a temporary file stream, without writing the contents to disk anywhere, then feed the stream to transferFile, as the file parameter? I know this is possible in PHP, but am not so sure about Swift.
I have come across a problem whereby my data exceeds the messaging limits. Therefore I either need to develop my own protocol to transfer the data, or somehow pass the data to transferFile.
I do not want to write my data to temporary files, transfer, and then delete them, as this is pretty dirty.
Why not just write the contents of the dictionary to a temporary file and pass the URL of that file to transferFile? I know you think that this is a "dirty" way to do things but since you need to pass a file URL to transferFile, that might be your only option - at least, that's the only option I know of ...
Related
So, I'm creating an app that works like a bot, it makes a call to an API from time to time, then it receives a response in a json-like format and saves it like this:
finalResult = RestClient.get( apiUrl, headers = apiHeaders )
jsonData = JSON.parse(ActiveSupport::Gzip.decompress(finalResult))
time = Time.now
File.write("public/#{time}.json", jsonData)
I'm using ActiveSupport to be able to parse this Gzip compressed data, since it's a lot of data, otherwise it takes forever to get it. Then I get the time the data was received, basically, and I use it to name the file so that I can keep good track of it.
What I need to do now is compress this .json file, if possible, into a .zip file(it can be .rar, or .7z, or .tz, whatever) before I upload it to my storage so it takes less space. Is there anyway that I can do something similar to File.write but to save it as a zipped json file? I already checked stuff like zlib and ruby-zip, but they only let me zip files that already "exist", so I can't save it as a zipped .json directly, I'd need to take the .json file and then zip it, but how could I do that if the name of the file is a Time.now and it always change?
I'd appreciate any help, thanks in advance :)
EDIT¹
Giving some more details that may help you to help me:
I created a controller and model to handle this, since I'll be using ActiveStorage. It's ResponsesController and Response model, and the only parameter that the Response model has is has_one_attached :json_file. I intend to use Heroku to handle the CRON job of calling the API and I'll upload the .json files(or .zip files) to an AWS storage.
I am receiving a text file from a socket over TCP/IP. There is no file extension (or filename for that matter) as the data is received as bytes. I can take the data from this (in the form of NSData) and load it into a UITextView and display it fine.
I want to persist this data to a file. However, I don't know what format the data is in (txt, rtf, doc, docx)? I assume it as .txt and save it in the documents directory, but is there a programmatic way of finding out for sure? I've looked around StackOverflow and at the documentation and haven't been able to find anything.
And is there a way to get the details of the file attributes like the file creation date.
Thanks in advance.
When you send a file over a TCP/IP connection, only the file contents will be converted to data and be passed across. If you want the filename,extension and the file attributes, then you will have to add those details separately and append it with the data to be sent. Then you can parse it at the receiver end and use the results inside your app.
You can choose the file type you want when you save the data, you can get attributes from file,please refer to Get file creation date.
I have an NSData containing a .webarchive blob that I'd like to load into a UIWebView. I know that this is possible (see this question), and I have it working if I first serialize it to disk and then load it with UIWebView's -loadRequest: method.
However, I'd prefer not to serialize to disk first since I already have the data in memory. I've tried to use -loadData:MIMEType:textEncodingName:baseURL: with the data, and various base URLs, but it always fails (nil, #"http://", the actual root path that the web archive contains, etc) to load.
Again, the same archive loads correctly if I bounce it to disk first and load via -loadRequest:, so I feel like something about the MIMEType (I'm using application/octet-stream) and/or the base URL is wrong. Anyone know what the incantation is?
Using -loadData:... will work. The MIME type specified must be application/x-webarchive (not the generic "octet-stream"). If this is set correctly, both the text encoding and base URL can be just supplied as nil.
there exist some sample code for an Http Server in the Dart:io section.
Now I will distribute images with this server. To achieve this, I read the requested image file and send its content to the client via request.response.write().
The problem is the format of the read data:
Either I read the image file as 16bit-String or as Byte Array. Neither of them is compatible to a raw 8-bit array, which I have to send to the client.
May someone help me?
There exist several kinds of write-methods in the response class.
write
writeCharCode
add
While "write" writes the data 'as seen', "writeCharCode" transforms the data back to raw-format. However, writeCharCode prepends some "magic byte" (C2) at the beginning, so it corrupts the data.
Another function, called add( List < int > ) processes the readAsBytes-result as desired.
Best regards,
Alex
I want to upload a file with cURL. Instead of an existing file on the hard disk, it is in memory(yes, I compose the file at runtime and want to eliminate the temporary file).
IIRC, with cURL, we can customize read callback function when sending ordinary post data. So is there similar mechanism we can use to customize callback function when reading a file, especially when used accompanied with multipart post?
Thanks and Best Regards!
Yes, curl_formadd's CURLFORM_STREAM is your friend:
Tells libcurl to use the CURLOPT_READFUNCTION callback to get data.
The parameter you pass to CURLFORM_STREAM is the pointer passed on to
the read callback's fourth argument. If you want the part to look like
a file upload one, set the CURLFORM_FILENAME parameter as well. Note
that when using CURLFORM_STREAM, CURLFORM_CONTENTSLENGTH must also be
set with the total expected length of the part. (Option added in
libcurl 7.18.2)