<commit_before>/*
 * Copyright (c) $year, <NAME> (DNT)
 *
 * https://github.com/Turistforeningen/turadmin
 */

module.exports = function (router) { // TODO: Pass router instead of app as argument in app.js
    "use strict";

    var gm = require('gm').subClass({imageMagick: true});
    var dms2dec = require('dms2dec');
    var path = require('path');
    var fs = require('fs');
    var _ = require('underscore');

    var ntbApi = require('./restProxy')(router, {ntbApiUri: process.env.NTB_API_URL, ntbApiKey: process.env.NTB_API_KEY});

    var options = {
        tmpDir: '/tmp', // tmp dir to upload files to
        uploadDir: '/tmp', // actual location of the file
        uploadUrl: '/uploads/', // end point for delete route
        maxPostSize: 11000000000, // 11 GB
        minFileSize: 1,
        maxFileSize: 10000000000, // 10 GB
        acceptFileTypes: /.+/i,
        inlineFileTypes: /\.(gif|jpe?g|png)/i,
        imageTypes:  /\.(gif|jpe?g|png)/i,
        accessControl: {
            allowOrigin: '*',
            allowMethods: 'OPTIONS, HEAD, GET, POST, PUT, DELETE',
            allowHeaders: 'Content-Type, Content-Range, Content-Disposition'
        },
        storage : {
            type : 'local' // local or aws
        }
    };

    var uploader = require('blueimp-file-upload-expressjs')(options);

    var s3Uploader = require('s3-uploader');

    var s3Client = new Upload('turadmin', {
        awsBucketUrl: 'https://s3-eu-west-1.amazonaws.com/turadmin/',
        awsBucketPath: process.env.AWS_BUCKET_PATH + '/',
        awsBucketAcl: 'public-read',
        versions: [
            {
                original: true
            },
            {
                maxHeight: 1040,
                maxWidth: 1040,
                suffix: '-large',
                quality: 80
            },
            {
                maxHeight: 780,
                maxWidth: 780,
                suffix: '-medium'
            },
            {
                maxHeight: 320,
                maxWidth: 320,
                suffix: '-small'
            }
        ]
    });


    // Return image path based on file upload module options and image name
    var getImagePath = function (image) {
        return path.join(options.uploadDir, image.name);
    };

    // Returns an image object with `geojson` property appended if image has gps info
    var getImagePosition = function (image, callback) {

        var imagePath = getImagePath(image);

        gm(imagePath).identify(function (err, value) {

            if (value['Properties']) {
                var lat = value['Properties']['exif:GPSLatitude'];
                var latRef = value['Properties']['exif:GPSLatitudeRef'];
                var lon = value['Properties']['exif:GPSLongitude'];
                var lonRef = value['Properties']['exif:GPSLongitudeRef'];

                if (!!lat && !!latRef && !!lon && !!lonRef) {
                    var dec = dms2dec(lat, latRef, lon, lonRef),
                        decLat = dec[0],
                        decLon = dec[1];

                    image.geojson = {
                        type: 'Point',
                        coordinates: [decLon, decLat]
                    };

                }
            }

            callback(err, image);

        });

    };

    var uploadImageToS3 = function (image, callback) {

        var imagePath = getImagePath(image);

        s3Client.upload(imagePath, function (err, images, exifData) {
            if (err) {
                console.error('Error when uploading picture to S3', err);
                callback(err, images);
            } else {
                fs.unlink(imagePath);
                callback(err, images);
            }
        });

    };

    var saveToNasjonalturbase = function (picture, callback) {

        // Need a request object for the ntbApi proxy
        var request = {
            method: 'POST',
            body: picture
        };

        ntbApi.makeApiRequest('/bilder', request, undefined, function (data, result) {
            var err = (result instanceof Error === true) ? result : null; // https://github.com/danwrong/restler#events see `complete: function(result, response)`
            if (err) {
                console.error('Error when saving picture to Nasjonal Turbase', err);
            } else {
                picture._id = data.document._id;
            }
            callback(err, picture);
        });

    };


    /**
     * Routes
     */

    var JFUM = require('jfum');
    var jfum = new JFUM();

    var async = require('async');

    router.options('/upload/picture', jfum.optionsHandler.bind(jfum));
    router.post('/upload/picture', jfum.postHandler.bind(jfum), function(req, res, next) {
        console.log('POST /upload/picture')
        async.mapSeries(req.jfum.files, function(image, cb) {
            if (image.error) {
                console.log(image.error);
                return process.nextTick(async.apply(cb, null, image));
            }
            console.log(image);
            s3Client.upload(image.file, function(err, image, exif) {
                console.log('s3Client cb');
                console.log(arguments);
            });

        }, function(images) {
            console.log(images);
        });
    });

    //router.post('/upload/picture', function(req, res) {
    //  uploader.post(req, res, function (obj) {
    //    getImagePosition(obj.files[0], function (err, image) {
    //        uploadImageToS3(image, function (err, images) {

    //            var originalImage = _.findWhere(images, {original: true});

    //            var picture = {img: [], geojson: image.geojson};
    //            picture.img = _.without(images, originalImage);
    //            picture.img.push(originalImage);

    //            saveToNasjonalturbase(picture, function (err, picture) {
    //                res.send(JSON.stringify({files: [picture]})); // Wrapping image in files object and array to match jQuery uploader plugin format
    //            });
    //        });
    //    });
    //  });
    //});

    return router;

};
<commit_msg>Refactor with jfum and s3-uploader<commit_after>/*
 * Copyright (c) $year, <NAME> (DNT)
 *
 * https://github.com/Turistforeningen/turadmin
 */

module.exports = function (router) { // TODO: Pass router instead of app as argument in app.js
    "use strict";

    var async = require('async');
    var JFUM = require('jfum');
    var Upload = require('s3-uploader');
    var dms2dec = require('dms2dec');

    var ntbApi = require('./restProxy')(router, {ntbApiUri: process.env.NTB_API_URL, ntbApiKey: process.env.NTB_API_KEY});
    var jfum = new JFUM();
    var s3Client = new Upload('turadmin', {
        awsBucketUrl: 'https://s3-eu-west-1.amazonaws.com/turadmin/',
        awsBucketPath: process.env.AWS_BUCKET_PATH,
        awsBucketAcl: 'public-read',
        returnExif: true,

        versions: [
            {
                original: true,
                awsImageAcl: 'private'
            },
            {
                maxHeight: 1040,
                maxWidth: 1040,
                suffix: '-large',
                quality: 80
            },
            {
                maxHeight: 780,
                maxWidth: 780,
                suffix: '-medium'
            },
            {
                maxHeight: 320,
                maxWidth: 320,
                suffix: '-small'
            }
        ]
    });

    var getImagePosition = function (exif) {
        var lat = exif['exif:GPSLatitude'],
            latRef = exif['exif:GPSLatitudeRef'],
            lon = exif['exif:GPSLongitude'],
            lonRef = exif['exif:GPSLongitudeRef'];

        if (!!lat && !!latRef && !!lon && !!lonRef) {
            var dec = dms2dec(lat, latRef, lon, lonRef),
                decLat = dec[0],
                decLon = dec[1];

            return {
                type: 'Point',
                coordinates: [decLon, decLat]
            };
        }
    };

    var saveToNasjonalturbase = function (picture, callback) {
        // Need a request object for the ntbApi proxy
        var request = {
            method: 'POST',
            body: picture
        };

        ntbApi.makeApiRequest('/bilder', request, undefined, function (data, result) {
            // https://github.com/danwrong/restler#events see `complete: function(result, response)`
            var err = (result instanceof Error === true) ? result : null;
            if (err) {
                console.error('Error when saving picture to Nasjonal Turbase', err);
            } else {
                picture._id = data.document._id;
            }
            callback(err, picture);
        });

    };


    /**
     * Routes
     */

    router.options('/upload/picture', jfum.optionsHandler.bind(jfum));
    router.post('/upload/picture', jfum.postHandler.bind(jfum), function(req, res, next) {
        console.log('POST /upload/picture')
        async.mapSeries(req.jfum.files, function(image, cb) {
            if (image.error) {
                console.log(image.error);
                return process.nextTick(async.apply(cb, null, {err: image.err, src: image}));
            }

            // Resize and upload image to S3
            s3Client.upload(image.path, {}, function(err, image, meta) {
                // @TODO handle error

                var geojson;

                // Remove original image
                image = image.splice(1);

                // Check and get image geoposition
                if (meta && meta.exif) {
                    geojson = getImagePosition(meta.exif);
                }

                // Post image to Nasjonal Turbase
                saveToNasjonalturbase({img: image, geojson: geojson}, cb);
            });

        }, function(err, images) {
            // @TODO handle error
            res.json({files: images});
        });
    });

    return router;

};

{'fraction_non_alphanumeric': 0.13007556675062973, 'fraction_numerical': 0.01017632241813602, 'mean_word_length': 1.6176160337552743, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2324113', 'n_tokens_mistral': 2976, 'n_tokens_neox': 2777, 'n_words': 638}