Mdlc
Mdlc

Reputation: 7288

Image handling node.js

I'm using this code to "generate" (modify an pre-made) android application project with a custom package name and some other variables. The code is working now (nodejs npm).

If you send a url request: http://magicarate.eu01.aws.af.cm/generate?appName=Hey&packageName=com.hey&webUrl=http://hey.com

It wil generate a application with app name "Hey", package name "com.hey" and weburl "http://hey.com" (obvious..)

The (pre made) application's structure is like:

 App
   |--src
   |  |--package
   |    |-- name
   |       |-- file.java
   |
   |--res
   |  |--layout
   |  |  |--layout.xml
   |  |  
   |  |--drawable
   |     |--logo.png         <---
   |     
   |manifest.xml 

I would like to change logo.png to a file provided by the user, or I make the drawable folder in the premade application empty, so the user provided file could be placed there.

My code to change the variables and folders (package names) is a little to long to post here. I've uploaded it to pastebin: http://pastebin.com/z1sX6NRj

I'm a pretty much a beginner, and I'm wondering what is the best way to do this and how to do this (getting the image from the user in the beginning), because it's different from the text replacing.

Upvotes: 0

Views: 162

Answers (1)

mak
mak

Reputation: 13405

Use the http module to download the image and then use the File System module to save it on disk. See also the Stream module.

var http = require('http');
var fs = require('fs');

var file = fs.createWriteStream("destination.png");
file.on('close' function() {
    // image saved
    // make a zip now
});
var request = http.get("http://your/img.png", function(response) {
  response.pipe(file);
});

In your case

function downloadAndSaveUserImage(url, done) {
    http.get(url, function(response) {
        var file = fs.createWriteStream("where/you/want/it/to/be.png");
        file.on('close' done);
        response.pipe(file);
    });
}

Replace sendContentAsZip(destDir, res); with

downloadAndSaveUserImage(req.query.imageUrl, function whenImageIsSaved() {
    sendContentAsZip(destDir, res);
});

You will want to check if the url is valid, etc.

Upvotes: 1

Related Questions