{"size": 3383, "ext": "js", "max_stars_repo_path": "app/controllers/azure.server.controller.js", "max_stars_repo_name": "biinio/CMS", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "app/controllers/azure.server.controller.js", "max_issues_repo_name": "biinio/CMS", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "app/controllers/azure.server.controller.js", "max_forks_repo_name": "biinio/CMS", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 26.8492063492, "max_line_length": 118, "alphanum_fraction": 0.5223174697}
/**
 * Created by Ivan on 8/27/15.
 */
/*
 *
 *	Exports the azure Manager Code
 *
 */
"use strict";
module.exports = function(){

    var azure = require('azure-storage');
    var util= require('util'),
        path = require('path');
    var functions = {};
    var stream = require('stream');
    //
    //Create a buquet credentials
    functions.createContainer=function(container,callbak){
        var blobSvc = azure.createBlobService(process.env.AZURE_STORAGE_ACCOUNT,process.env.AZURE_STORAGE_ACCESS_KEY);
        blobSvc.createContainerIfNotExists(container, {publicAccessLevel : 'blob'}, function(error, result, response){
            if(!error){
                callbak(error,result);
            }else{
                callbak(null,{status:"Container Ready Exist"});
            }

        });
    };

    //
    //Put the Object in to a buquet
    functions.uploadObjectToContainer=function(container,objKey,buffer,imageFormat,callback){
        var fileBuffer = new Buffer(buffer);
        var blobSvc = azure.createBlobService(process.env.AZURE_STORAGE_ACCOUNT,process.env.AZURE_STORAGE_ACCESS_KEY);

        blobSvc.createBlockBlobFromStream(
            container,
            objKey,
            new ReadableStreamBuffer(fileBuffer),
            fileBuffer.length,
            { contentTypeHeader:'image/'+imageFormat },
            function(error,data)
            {
                if(!error)
                {
                    callback(data);
                }
                else
                {
                    console.log(error);
                    throw error;
                }
            });
    };

    //Readable buffer
    var ReadableStreamBuffer = function(fileBuffer) {

        var that = this;
        stream.Stream.call(this);
        this.readable = true;
        this.writable = false;

        var frequency = 50;
        var chunkSize = 1024;
        var size = fileBuffer.length;
        var position = 0;

        var buffer = new Buffer(fileBuffer.length);
        fileBuffer.copy(buffer);

        var sendData = function() {
            if(size === 0) {
                that.emit("end");
                return;
            }

            var amount = Math.min(chunkSize, size);
            var chunk = null;
            chunk = new Buffer(amount);
            buffer.copy(chunk, 0, position, position + amount);
            position += amount;
            size -= amount;

            that.emit("data", chunk);
        };

        this.size = function() {
            return size;
        };

        this.maxSize = function() {
            return buffer.length;
        };

        this.pause = function() {
            if(sendData) {
                clearInterval(sendData.interval);
                delete sendData.interval;
            }
        };

        this.resume = function() {
            if(sendData && !sendData.interval) {
                sendData.interval = setInterval(sendData, frequency);
            }
        };

        this.destroy = function() {
            that.emit("end");
            clearTimeout(sendData.interval);
            sendData = null;
            that.readable = false;
            that.emit("close");
        };

        this.setEncoding = function(_encoding) {
        };

        this.resume();
    };

    util.inherits(ReadableStreamBuffer, stream.Stream);
    return functions;
};