Ejemplo n.º 1
0
/**
 *	Move modified files from a temporary (working) repository on top of another (template) repository
 *	@param $param[1] temporary repository
 *	@param $param['repo'] repository to switch to
 *	@param $param['clean_before'] run "make clean" before determining the modified files (default: true)
 *	@param $param['clean_after'] run "make clean" after determining the modified files (default: true)
 */
function api_post_temp_switch_repo($param = array())
{
    $temp = $param[1];
    if (!@is_dir(tmp_dir($temp))) {
        router_error_404('Cannot get ' . $temp);
    }
    if (empty($param['repo'])) {
        router_error_400('Required parameter repo missing or empty');
    } else {
        $repo = $param['repo'];
    }
    // client can specify whether to run "make clean" before or after the switch
    if (isset($param['clean_before'])) {
        $clean_before = (bool) $param['clean_before'];
    } else {
        $clean_before = true;
    }
    if (isset($param['clean_after'])) {
        $clean_after = (bool) $param['clean_after'];
    } else {
        $clean_after = true;
    }
    // create a new repository under a temporary name
    $staging = get_repo($repo);
    if ($staging === false) {
        router_error_404('Cannot get ' . $repo);
    }
    // clean, if requested
    if ($clean_before) {
        make_run($temp, 'clean');
    }
    // copy the modified files over
    $old_umask = @umask(00);
    foreach (repo_get_modified_files($temp) as $fn) {
        // make sure the containing directories exist
        create_containing_dir(tmp_dir($staging) . '/' . $fn);
        // copy
        @copy(tmp_dir($temp) . '/' . $fn, tmp_dir($staging) . '/' . $fn);
    }
    @umask($old_umask);
    // remove original repository
    if (false === rm_recursive(tmp_dir($temp))) {
        router_error_500('Cannot delete ' . $temp);
    }
    // move new repository to location of original repository
    if (false === @rename(tmp_dir($staging), tmp_dir($temp))) {
        router_error_500('Cannot rename ' . $staging . ' to ' . $temp);
    }
    // clean, if requested
    if ($clean_after) {
        make_run($temp, 'clean');
    }
    return true;
}
Ejemplo n.º 2
0
function inject_uploaded_file($tmp_key, $file, $auto_convert = true)
{
    // establish destination filename
    $dest_fn = get_uploaded_file_dest_fn($tmp_key, $file['name'], $file['type'], $file['tmp_name']);
    if ($dest_fn === false) {
        return array();
    }
    // make sure the containing directories exist
    create_containing_dir(tmp_dir($tmp_key) . '/' . $dest_fn);
    // move to destination
    if (false === @move_uploaded_file($file['tmp_name'], tmp_dir($tmp_key) . '/' . $dest_fn)) {
        return array();
    }
    if ($auto_convert) {
        // convert Word documents instantaneously to Markdown
        $start = time();
        if (filext($dest_fn) === 'docx') {
            make_run(tmp_dir($tmp_key), 'markdowns');
            // XXX (later): run "make clean" here?
        }
        $modified_after = repo_get_modified_files_after($tmp_key, $start - 1);
        // make sure the destination filename is part of th array
        if (is_array($modified_after) && !in_array($dest_fn, $modified_after)) {
            $modified_after[] = $dest_fn;
        }
        return $modified_after;
    }
    return array($dest_fn);
}