<commit_before>/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

/*
 * Copyright (c) 2014, Joyent, Inc.
 */

module.exports = {
    list: list,
    get: get,
    activateImage: activateImage,
    action: action,
    update: update,
    uploadImage: uploadImage,
    importImage: importImage,
    aclAction: aclAction
};


var restify = require('restify');
var IMGAPI = require('sdc-clients/lib/imgapi');

function action(req, res, next) {

    var act = req.params.action;

    if (act === 'upload') {
        return uploadImage(req, res, next);
    }

    if (act === 'activate') {
        return activateImage(req, res, next);
    }

    if (act === 'disable') {
        return disableImage(req, res, next);
    }

    if (act === 'enable') {
        return enableImage(req, res, next);
    }

    if (act === 'import') {
        return importImage(req, res, next);
    }

    if (act === 'importRemote') {
        return importRemote(req, res, next);
    }

    return next(new restify.ConflictError('Action not supported'));
}

function update(req, res, next) {
    req.sdc[req.dc].imgapi.updateImage(req.params.uuid, req.body, function (err, image) {
        if (err) {
            req.log.fatal(err);
            return next(err);
        } else {
            res.send(image);
            return next();
        }
    });
}

function list(req, res, next) {
    var client = req.sdc[req.dc].imgapi;

    if (req.query.repository) {
        var repository = req.query.repository;
        delete req.query.repository;
        client = IMGAPI.createClient({ url: repository });
    }

    client.listImages(req.params, function (err, images, listImagesRes) {
        if (err) {
            req.log.fatal(err);
            return next(err);
        } else {
            res.send(images);
            return next();
        }
    });
}

function get(req, res, next) {
    req.sdc[req.dc].imgapi.getImage(req.params.uuid, function (err, image, getImagesRes) {
        if (err) {
            req.log.fatal(err);
            return next(err);
        } else {
            res.send(image);
            return next();
        }
    });
}

function disableImage(req, res, next) {<commit_msg>ADMINUI-1915 imgapi doesn't support querying ListImages?uuid=X, adminui needs workaround<commit_after>/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

/*
 * Copyright (c) 2014, Joyent, Inc.
 */

module.exports = {
    list: list,
    get: get,
    activateImage: activateImage,
    action: action,
    update: update,
    uploadImage: uploadImage,
    importImage: importImage,
    aclAction: aclAction
};


var restify = require('restify');
var IMGAPI = require('sdc-clients/lib/imgapi');

function action(req, res, next) {

    var act = req.params.action;

    if (act === 'upload') {
        return uploadImage(req, res, next);
    }

    if (act === 'activate') {
        return activateImage(req, res, next);
    }

    if (act === 'disable') {
        return disableImage(req, res, next);
    }

    if (act === 'enable') {
        return enableImage(req, res, next);
    }

    if (act === 'import') {
        return importImage(req, res, next);
    }

    if (act === 'importRemote') {
        return importRemote(req, res, next);
    }

    return next(new restify.ConflictError('Action not supported'));
}

function update(req, res, next) {
    req.sdc[req.dc].imgapi.updateImage(req.params.uuid, req.body, function (err, image) {
        if (err) {
            req.log.fatal(err);
            return next(err);
        } else {
            res.send(image);
            return next();
        }
    });
}

function list(req, res, next) {
    var client = req.sdc[req.dc].imgapi;

    if (req.query.repository) {
        var repository = req.query.repository;
        delete req.query.repository;
        client = IMGAPI.createClient({ url: repository });
    }

    if (req.params.uuid) {
        req.sdc[req.dc].imgapi.getImage(req.params.uuid, function (err, image, getImagesRes) {
            if (err) {
                req.log.fatal(err);
                return next(err);
            } else {
                res.send([image]);
                return next();
            }
        });
    } else {
        client.listImages(req.params, function (err, images, listImagesRes) {
            if (err) {
                req.log.fatal(err);
                return next(err);
            } else {
                res.send(images);
                return next();
            }
        });
    }
}

function get(req, res, next) {
    req.sdc[req.dc].imgapi.getImage(req.params.uuid, function (err, image, getImagesRes) {
        if (err) {
            req.log.fatal(err);
            return next(err);
        } else {
            res.send(image);
            return next();
        }
    });
}

function disableImage(req, res, next) {
{'fraction_non_alphanumeric': 0.14024864024864026, 'fraction_numerical': 0.003885003885003885, 'mean_word_length': 2.021713615023474, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '173969', 'n_tokens_mistral': 1644, 'n_tokens_neox': 1582, 'n_words': 403}