<commit_before>// upgrader.js
//
// Do in-place upgrades of activity objects as needed
//
// Copyright 2011, 2013 E14N https://e14n.com/
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.


var os = require("os"),
    Step = require("step"),
    path = require("path"),
    _ = require("underscore"),
    thumbnail = require("./thumbnail"),
    Image = require("./model/image").Image,
    mover = require("./mover");

// Upgrade image data.

var upgradeImage = function(img, callback) {

    if (img._slug && _.isObject(img.image) && !_.has(img.image, "width")) {

        Step(
            function() {
                autorotateImage(img, this);
            },
            function(err) {
                if (err) throw err;
                thumbnail.addImageMetadata(img, Image.uploadDir, this);
            },
            function(err) {
                if (err) throw err;
                img.save(this);
            },
            function(err, saved) {
                callback(err);
            }
        );
    } else {
        callback(null);
    }
};

var autorotateImage = function(img, callback) {

    var fname = path.join(Image.uploadDir, img._slug),
        tmpname = path.join(os.tmpdir(), img._uuid);

    Step(
        function() {
            thumbnail.autorotate(fname, tmpname, this);
        },
        function(err) {
            if (err) throw err;
            mover.safeMove(tmpname, fname, this);
        },
        callback
    );
};

var upgradePerson = function(person, callback) {

    var img,
        urlToSlug = function(person, url) {
            var start = url.indexOf("/" + person.preferredUsername + "/");
            return url.substr(start + 1);
        };

    // Automated update from v0.2.x, which had no thumbnailing of images
    // This checks for local persons with no "width" in their image
    // and tries to update the user data.

    if (person._user && _.isObject(person.image) && !_.has(person.image, "width")) {

        Step(
            function() {
                Image.search({"_slug": urlToSlug(person, person.image.url)}, this);
            },
            function(err, images) {
                if (err) throw err;
                if (!images || images.length != 1) {
                    throw new Error("Wrong number of images");
                }
                img = images[0];
                autorotateImage(img, this);
            },
            function(err) {
                if (err) throw err;
                thumbnail.addAvatarMetadata(img, Image.uploadDir, this);
            },
            function(err) {
                if (err) throw err;
                img.save(this);
            },
            function(err) {
                if (err) throw err;
                person.image = img.image;
                if (!person.pump_io) {
                    person.pump_io = {};
                }
                person.pump_io.fullImage = img.fullImage;
                person.save(this);
            },
            function(err, saved) {
                callback(err);
            }
        );
    } else {
        callback(null);
    }
};

module.exports = {
    upgradeImage: upgradeImage,
    upgradePerson: upgradePerson
};<commit_msg>Change to use slug; change order of image loading<commit_after>// upgrader.js
//
// Do in-place upgrades of activity objects as needed
//
// Copyright 2011, 2013 E14N https://e14n.com/
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.


var os = require("os"),
    Step = require("step"),
    path = require("path"),
    _ = require("underscore"),
    thumbnail = require("./thumbnail"),
    Image = require("./model/image").Image,
    mover = require("./mover");

// Upgrade image data.

var upgradeImage = function(img, callback) {

    if (img._slug && _.isObject(img.image) && !_.has(img.image, "width")) {

        Step(
            function() {
                autorotateImage(img, this);
            },
            function(err) {
                if (err) throw err;
                thumbnail.addImageMetadata(img, Image.uploadDir, this);
            },
            function(err) {
                if (err) throw err;
                img.save(this);
            },
            function(err, saved) {
                callback(err);
            }
        );
    } else {
        callback(null);
    }
};

var autorotateImage = function(img, callback) {

    var fname = path.join(Image.uploadDir, img._slug),
        tmpname = path.join(os.tmpdir(), img._uuid);

    Step(
        function() {
            thumbnail.autorotate(fname, tmpname, this);
        },
        function(err) {
            if (err) throw err;
            mover.safeMove(tmpname, fname, this);
        },
        callback
    );
};

var upgradePerson = function(person, callback) {

    var img,
        urlToSlug = function(person, url) {
            var start = url.indexOf("/" + person.preferredUsername + "/");
            return url.substr(start + 1);
        };

    // Automated update from v0.2.x, which had no thumbnailing of images
    // This checks for local persons with no "width" in their image
    // and tries to update the user data.

    if (person._user && _.isObject(person.image) && !_.has(person.image, "width")) {

        Step(
            function() {
                var slug = urlToSlug(person, person.image.url);
                Image.search({"_slug": slug}, this);
            },
            function(err, images) {
                if (err) throw err;
                if (!images || images.length != 1) {
                    throw new Error("Wrong number of images");
                }
                img = images[0];
                autorotateImage(img, this);
            },
            function(err) {
                if (err) throw err;
                thumbnail.addAvatarMetadata(img, Image.uploadDir, this);
            },
            function(err) {
                if (err) throw err;
                person.image = img.image;
                if (!person.pump_io) {
                    person.pump_io = {};
                }
                person.pump_io.fullImage = img.fullImage;
                person.save(this);
            },
            function(err) {
                if (err) throw err;
                img.save(this);
            },
            function(err, saved) {
                callback(err);
            }
        );
    } else {
        callback(null);
    }
};

module.exports = {
    upgradeImage: upgradeImage,
    upgradePerson: upgradePerson
};
{'fraction_non_alphanumeric': 0.12199015564719969, 'fraction_numerical': 0.0055873353731541835, 'mean_word_length': 1.6295907660020987, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '2919902', 'n_tokens_mistral': 2115, 'n_tokens_neox': 2020, 'n_words': 569}