Nodejs upload images to other server API - php

I was wondering if any one can help with following issue.
I have an Api with node/express for image uploads
server1
var upload = multer({ dest: 'uploads/'})
app.post('/api/upload', upload.single('file'), function(req, res, next) {
if (req.file && req.file.path) {
//used to have imgur upload
//imgur.uploadFile(req.file.path)
// .then(function (json) {
//});
}
});
server2
I have PHP Api for image uploads as well which I have set up for self-hosting using pictshare. I want to redirect file uploads to new API by redirecting uploads to PHP Api server.
Have tried multer, multiparty, needle, request and various other methods... but somehow couldn't figure out.
Is there way to direct multer to new destination?
File is being saved in uploads/ folder, maybe better would be to upload/direct that file to new server and return new url from server2 ?
Looked around for pipelining upload images, with not much luck..
note: server1 api is being used by mobile app so wouldn't want to release app update if it can be handled from server side.

To upload a file (image or whatever) you need to have it in your drive first (or in memory, but I will tackle the drive case first). Then you can read it and send it.
The APIs that receive files use to receive forms. Usually they expect to receive a form where the attachment comes as a file field.
Here you can see a working example uploading an existing file in my drive to an external API by using the 'superagent' library for Node.
const
fs = require('fs'),
agent = require('superagent');
const stream = fs.createReadStream('path/to/downloaded/file');
agent.post(`urlOfApi/uploadFileEndpoint`)
.type('form')
.attach('file', stream.path);

Related

Is there special markup for downloading video and audio in iOS?

I'm making an app in ionic phonegap where I'd like to offer streaming and downloading the video and audio files for offline use.
In Desktop and Android, this is easy. All I have to do is link directly to the file and modify the header directives in php to force the download.
In iPhone/iOs, clicking the same link tries to open the file in a player immediately and doesn't download the file. Furthermore, it doesn't actually play the file. It just loads an empty video player.
I've tried googling this and looking it up on SO, but I can only find guides for downloading and storing the file within the app.
Such as: iOS - Download Video
But that's not what I need. I don't want to reinvent the wheel and turn my app into a file manager. I need the phone to store it natively in cameraroll or prompt the user to load the file in whatever app they normally use for offline video.
I did find a user-level guide that shows that this is possible, but it doesn't say how we can make this possible as developers:
http://osxdaily.com/2013/05/28/save-videos-from-mail-ios/
How do I mark up a video link so that iOS knows to download it locally?
Please check File Transfer Cordova plugin to see if it works on you.
http://plugins.cordova.io/#/package/org.apache.cordova.file-transfer
And here goes an example code snippet:
var fileTransfer = new FileTransfer();
var uri = encodeURI("http://some.server.com/example_file.mp4");
fileTransfer.download(
uri,
fileURL,
function(entry) {
console.log("download complete: " + entry.toURL());
},
function(error) {
console.log("download error source " + error.source);
console.log("download error target " + error.target);
console.log("upload error code" + error.code);
},
false, null
);
EDIT:
Sorry for missing "cameraroll" in your question. I believe there's no direct way to save images from your url to your Camera Roll. However there's an indirect way for this. You can possibly load your image on the HTML5 canvas in your app. After that, just save its base64 formatted data_url to your camera roll.
Check this answer for more informations: Save to camera roll from path/URL in PhoneGap

blueimp/jQuery-File-Upload direct upload to S3

I'm trying to implement blueimp jquery uploader. I want it to upload directly to S3 (without PHP script in the middle). I found an example here and followed the steps and it worked (partially). But I could not find anything good for muti-file upload.
What I want to do is to have multiple file uploader with a UI feedback just like in this example.
Does anybody have an example to follow for this scenario?
Thanks for your help.
I couldn't get blueimp to perfectly upload to s3, it would still take advantage of my servers tmp folder.
I ended up switching over to plupload which does upload directly to s3, and has a built in UI ( I just created my own to match the rest of my site).
Uploading Directly to S3
Full Example
I'm not using the multiple file feature but you could do something like
foreach file you are uploading:
var multipart_params = {
'key': config['key'],
'Filename': config['key'],
'acl': 'public-read',
'Content-Type': '',
'AWSAccessKeyId': config['awskey'],
'policy': config['policy'],
'signature': config['signature']
};
up.settings.multipart_params = multipart_params;
up.start();
Hope that helps :)

Create a http proxy in php for uploading images using 'iframe'

Use Case: I am working on an image uploader which uses ajax upload function.I want to upload images to a subdomain user creates on the website.For example,when the user creates a domain on the website I copy a php script for uploading images to the new domain viz image-cropping.php.I want to send a request to this file when the user uploads any image to his domain.
Issue:When I try to upload an image I get Error: Permission denied to access property 'readyState'.My calling js file is on xyz.google.com and the upload php script is on abc.google.com.
Research
After doing some googling and research I learnt javascript won't allow to send request cross domain and it needs a http proxy to handle this.Here is the code I have tried.The script to run the ajax uploader.In action I have the path to file on other domain(the path is built dynamically).
new AjaxUpload(btnUpload, {
action: 'includes/modules/domain_creation/proxy.php',
name: 'image',
onSubmit: function(file, ext){
if (! (ext && /^(jpg|png|jpeg|gif|JPG|JPEG|PNG|GIF)$/.test(ext))){
alert('Only JPG, PNG or GIF files are allowed');
return false;
}
$('#thumbImg').html('<img src="http://localhost/gobiggi_VS_2_2/images/appImages/imageLoader.png" />');
},
1) Am I doing it in the right way(working on it for first time)?Is the proxy actually needed?2)How and where can I set the proxy so that the error permission can be negotiated?3)What security issues does it open up(Is it safe?If not what's an alternative)?Any pointers or suggestions would be helpful for me.Thank you for your time.
Update:
I am using this proxy script for uploading the image.Part of the code is
$domainUsername = $_SESSION['domainUsername'];
$domainNameWeb = $_SESSION['domainName'];
//$fileParameterProxy = $_FILES['image'];
//Destination URL: Where this proxy leads to the upload script
$destinationURL = 'http://www.'.$domainNameWeb.'/'.$domainUsername.'/upload.php';
//The only domain from which requests are authorized.
$RequestDomain = 'abc.net';
Now I don't get the Error for permission but I am not able to get the image on to the server.When I try to do print_r($_FILES) I get a blank array on my upload script.
I believe I am missing something!!Can someone please correct?
Thank you for your time!
1 and 2) You have to set your proxy as action, because that is the place where you are allowed to upload the files. The proxy then will do the request to the other domain, where it can send the files to.
3) Depends on your proxy implementation. You should avoid to store the files locally or execute/include anything from user input, like always when writing php scripts. Directly send the tmp file to your destination server, this will also be the fastest implementation.

upload file to a server using lua

I have been trying to upload a file to a webserver using LUA.
My problem is that I want to upload a file using LUA to webserver which mimics uploading a file like from a browser.
I was successfully able to upload file using server, where I can do file_get_contents('php://input'), where i get contents and mime_decode on it and save that file.
But, i want to achieve where i can do like $_FILES['file_name'], on server side using lua.
so does anybody have idea how to do this in LUA?
Regards.
You can use the HTTP sockets library for sending HTTP requests and ltn12 library for filters (file input). Both should be provided with Lua.
http = require("socket.http")
ltn12 = require("ltn12")
http.request{
url = "url://to.server/upload/script.php",
method = "POST",
headers = {
["Content-Type"] = "multipart/form-data",
["Content-Length"] = sizeOfFile
},
source = ltn12.source.file(io.open(pathToLocalFile)),
sink = ltn12.sink.table(response_body)
}
print(response_body[1]) --response to request

How do I directly upload a file stream to Flickr using a CURL POST?

I'm writing a web app that at one point allows a user to upload a photo to a flickr account (mine). I want to do this without saving the intermediate image on the server my web app is on.
What I've got so far is a page which implements phpFlickr and accepts a POST from a simple html form. I use $_FILES['file']['tmp_name'] as the path for phpFlickr to use. Here's the code:
<?php
require_once("phpFlickr.php");
$f = new phpFlickr("apikey", "secret", true);
$_SESSION['phpFlickr_auth_redirect'] = "post_upload.php";
$myPerms = $f->auth("write");
$token = $f->auth_checkToken();
$phid = $f->sync_upload($_FILES['file']['tmp_name']);
echo "Uploading Photo..." . $phid;
?>
I'm guessing that the tmp file is being lost because of the redirect that happens when $f->auth("write") is called, but I don't know. Is there a way to preserve it? Is there any way to do this without saving the file to the server?
Answer: There is No way to directly upload a file to Flickr without saving it as an intermediate file.
I've moved on to using move_uploaded_file() followed by a flickr API call, and its working perfectly.
I've also managed to get it to play nice with the excellent Jquery Uploadify, which lets me send multiple files to it in one go.

Categories