Lua HTTP download and save to file - sockets

I'm trying to download and then save the contents to a xml file. The code I got is:
local filePath = currentDir().."/file.xml"
local http = require("socket.http")
local xFile = io.open(filePath, "w")
local save = ltn12.sink.file(xFile)
http.request{addr, sink = save }
print("Done!")
It runs but the file is still empty. Can I get some help here please?

Its a syntactical mistake. You mixed two styles of calling http.request. Use
http.request{url = addr, sink = save }

Related

Problems in descompression file - Out of memory

I have a sales app develpmented in Flex Builder. For use photos by off-line way, i upload a compressed file with all photos to the site and when need i sincronize with my smartphone. In that process is made a download to the dispositive and exist a responsable plugin to descompress the archive and disponibilize it in apropriate folder to later use.
That compressed file has 500MB size - 6700 photos approximately - and in Flex i have no type of problem to do that.
I'm rewriting the app in Flutter, using a archive package, dont getting the same results. In the descompress process i have problem with Out of memory.
Somebody already faced for something like that ?
Is there an best way to do this ?
I already tried other two alternatives:
- To use the Image.network, but, one of requires is work off-line.
- To save all the photos in assets folder, but i think that is not the best alternative, because the app will get bigger
Thank you in advance for.
Follow the error message and the code
code:
unarchiveAndSave()async{
var zippedFile = await initDir();
var bytes = zippedFile.readAsBytesSync();
var archive = ZipDecoder().decodeBytes(bytes);
for (var file in archive) {
var fileName = '$_dir/${file.name}';
if (file.isFile) {
var outFile = File(fileName);
print('File:: ${outFile.path}');
outFile = await outFile.create();
await outFile.writeAsBytes(file.content);
}
}
print('terminei de descompactar os arquivos');
}

Swift 3 Mac OS Copy Audio File to NSPasteboard

I am trying to copy an audio file to the NSPasteboard so that it can be pasted somewhere else on the computer or into another program like Ableton or Pro Tools. Here is how I am getting the url of the file. (An example url d after casting to string is: file:///Users/ben/Music/Ableton/User%20Library/Vox.wav )
let url = directoryItems?[tableview.selectedRow].url
let urlString = (url?.absoluteString)! as String
let pb = NSPasteboard.general()
let pasted = pb.writeFileContents(urlString)
It is not being copied to the pasteboard (pasted is set to false) and I can not find any resources that talk about writing audio files to the NSPasteboard. Any help would be greatly appreciated.
Edit:
I also tried using the url instead of the string and had the same outcome
let pb = NSPasteboard.general()
let pasted = pb.writeObjects([url as! NSPasteboardWriting])
To copy a file in such a way that you can paste it in the Finder, you'd want a file URL, not a string.
As for copying the music into a music editor, presumably you'd need to load the music file yourself into some waveform format that can be pasted into that editor.

Sinatra example code to download a large file

I started using sinatra,
Right now I'm using the following code to handle file downloads,
It works great for small files, but when it comes to large files > 500MB
The connection disconnects in the middle.
dpath = "/some root path to file"
get '/getfile/:path' do |path|
s = path.to_s
s.gsub!("-*-","/")
fn = s.split("/").last
s = dpath +"/"+ s
send_file s,:filename => fn
end
Two things:
What does your validate method do? If it's trying to open the file in memory, you might be running out of ram on your server (especially with large files).
Where are you setting fn ? It's a local variable inside the get scope and there's nothing setting it in your code example.

Wav file tranfer sockets

Solution (in a way): I just added atime sleep although the half file still got corrupted but at least now i transfered all the 1.19 mb .
Removing completely base64 seems to be the answer :D
I am trying to create a script where a user can tranfer a wav file .
This is my current code both client side and server side . The problem is
that my data is corrupted at the end .
Also when i see all the data transfered i am closing the server
Ps. I that file is bad name variable but i am using it only now during the tests
Thanks in advance .
Client Side:
class SockThread(QtCore.QThread):
def create_r(self, filename):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.filename = filename
def run(self):
self.sock.connect(("127.0.0.1", 8000))
wf = open(self.filename, 'rb')
print("OK")
for line in wf:
self.sock.send(base64.b64encode(line))
wf.close()
print("OK")
Server Side:
file = open("sample.wav", "wb")
self.connection = True
while self.connection is True:
try:
data = self.socket.recv(1024)
print(data)
file.write(base64.b64decode(data))
except:
break
I think you need to read the entire file from the socket and then b64decode it, doing it in chunks of 1024 byte may not produce the same result.

Titanium Convert TiFile Object to TiFileSystemFile Object

How to save the recorded audio file which is recorded using sound_record.js from Titanium KickenSink Source.
We have,
var file // global variable.
file = recording.stop(); // file will have recorded content which we will convert into media.sound in order to play.
var newDir = Titanium.Filesystem.getFile(Titanium.Filesystem.resourcesDirectory,'audioclips');
Ti.API.info("Created mydir: " + newDir.createDirectory());
var newFile = Titanium.Filesystem.getFile(newDir.nativePath,'newfile.wav');
newFile.write(file.read());
But i am able to save the file which is recorded?
I am not getting how to save this recorded file, I dont knw where i am going wrong.
Please Help,
Thanks in Advance.
I am guessing your are having problem with saving the file. In last line just change
newFile.write(file.read());
to
newFile.write(file);
This should do the trick for you.
Hope this helps.
Edited:
For saving the file try this code:
var newFile =Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,'your_file.wav');
// Write the data.
newFile.write( file );