<commit_before>//书籍创建、管理
var _ = require('../base/util');
var parse = require('co-busboy');
var fs = require('fs');
var BookCtrl = require('../base/book');

/**
 * 同步图书代码
 */
function *sync(body){
    var userName = this.session['user'] && this.session['user'].name || '';
    if(body){
        userName = body.owner;
    }else{
        body = yield this.request.body;
        this.log('[/api/book/sync] :');
        this.log(body);
    }

    var id = body.id;

    var mBook = this.model.book;
    //事件记录
    var mHistory = this.model.history;
    var book = this.book;

    //没有绑定github仓库
    if(!book.bindGithub){
        _.error.bind(this)('请先绑定github仓库');
        return false;
    }

    yield mHistory.add(book.id,'action','开始同步 github 仓库内容到服务器上',userName);

    var bookCtrl = new BookCtrl({
        user:book.userName,
        book: book.uri,
        githubUrl:book.githubUrl,
        oss:this.oss,
        bucket:this.config.ossBuckets.book,
        env:this.config.env,
        data: book,
        apebookHost: this.config.host,
        assetHost: this.config.assetHost,
        ctx: this
    });

    //同步代码
    var pullResult = yield bookCtrl.pull();
    // var pullResult = {
    //     success: true,
    //     change:true,
    //     output: "From https://github.com/apebook/guide * branch            master     -> FETCH_HEAD\nSUMMARY.md | 1 +"
    // };
    this.log(pullResult);
    //pullResult.change = true;
    if(!pullResult.success){
        this.error(pullResult);
        yield mHistory.add(book.id,'error','github 内容同步失败，失败原因如下：<br />'+pullResult,userName);
    }else{
        yield mHistory.add(book.id,'github','github 内容同步成功<br />'+ pullResult.output||'',userName);

        //存在文件变更，渲染html
        if(pullResult.change){
            var renderResult = yield bookCtrl.render();
            //渲染失败
            if(!renderResult.success){
                this.log(renderResult);
                pullResult = {success: false,msg:renderResult.msg || '渲染失败'};
                yield mHistory.add(book.id,'error','渲染失败！错误信息如下：<br/><span class="error-msg">'+renderResult.error.message+'</span>',userName);
            }else{
                yield mHistory.add(book.id,'success','图书渲染成功',userName);

                //封面信息<commit_msg>change true<commit_after>//书籍创建、管理
var _ = require('../base/util');
var parse = require('co-busboy');
var fs = require('fs');
var BookCtrl = require('../base/book');

/**
 * 同步图书代码
 */
function *sync(body){
    var userName = this.session['user'] && this.session['user'].name || '';
    if(body){
        userName = body.owner;
    }else{
        body = yield this.request.body;
        this.log('[/api/book/sync] :');
        this.log(body);
    }

    var id = body.id;

    var mBook = this.model.book;
    //事件记录
    var mHistory = this.model.history;
    var book = this.book;

    //没有绑定github仓库
    if(!book.bindGithub){
        _.error.bind(this)('请先绑定github仓库');
        return false;
    }

    yield mHistory.add(book.id,'action','开始同步 github 仓库内容到服务器上',userName);

    var bookCtrl = new BookCtrl({
        user:book.userName,
        book: book.uri,
        githubUrl:book.githubUrl,
        oss:this.oss,
        bucket:this.config.ossBuckets.book,
        env:this.config.env,
        data: book,
        apebookHost: this.config.host,
        assetHost: this.config.assetHost,
        ctx: this
    });

    //同步代码
    var pullResult = yield bookCtrl.pull();
    // var pullResult = {
    //     success: true,
    //     change:true,
    //     output: "From https://github.com/apebook/guide * branch            master     -> FETCH_HEAD\nSUMMARY.md | 1 +"
    // };
    this.log(pullResult);
    pullResult.change = true;
    if(!pullResult.success){
        this.error(pullResult);
        yield mHistory.add(book.id,'error','github 内容同步失败，失败原因如下：<br />'+pullResult,userName);
    }else{
        yield mHistory.add(book.id,'github','github 内容同步成功<br />'+ pullResult.output||'',userName);

        //存在文件变更，渲染html
        if(pullResult.change){
            var renderResult = yield bookCtrl.render();
            //渲染失败
            if(!renderResult.success){
                this.log(renderResult);
                pullResult = {success: false,msg:renderResult.msg || '渲染失败'};
                yield mHistory.add(book.id,'error','渲染失败！错误信息如下：<br/><span class="error-msg">'+renderResult.error.message+'</span>',userName);
            }else{
                yield mHistory.add(book.id,'success','图书渲染成功',userName);

                //封面信息
{'fraction_non_alphanumeric': 0.16104702750665484, 'fraction_numerical': 0.00044365572315882877, 'mean_word_length': 2.202414772727273, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 2, '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': '2439274', 'n_tokens_mistral': 1585, 'n_tokens_neox': 1626, 'n_words': 240}