예제 #1
0
파일: zimg.c 프로젝트: Alexniver/zimg
/**
 * @brief save_img Save buffer from POST requests
 *
 * @param thr_arg Thread arg struct.
 * @param buff The char * from POST request
 * @param len The length of buff
 * @param md5 Parsed md5 from url
 *
 * @return 1 for success and -1 for fail
 */
int save_img(thr_arg_t *thr_arg, const char *buff, const int len, char *md5)
{
    int result = -1;

    LOG_PRINT(LOG_DEBUG, "Begin to Caculate MD5...");
    md5_state_t mdctx;
    md5_byte_t md_value[16];
    char md5sum[33];
    int i;
    int h, l;
    md5_init(&mdctx);
    md5_append(&mdctx, (const unsigned char*)(buff), len);
    md5_finish(&mdctx, md_value);

    for(i=0; i<16; ++i)
    {
        h = md_value[i] & 0xf0;
        h >>= 4;
        l = md_value[i] & 0x0f;
        md5sum[i * 2] = (char)((h >= 0x0 && h <= 0x9) ? (h + 0x30) : (h + 0x57));
        md5sum[i * 2 + 1] = (char)((l >= 0x0 && l <= 0x9) ? (l + 0x30) : (l + 0x57));
    }
    md5sum[32] = '\0';
    strcpy(md5, md5sum);
    LOG_PRINT(LOG_DEBUG, "md5: %s", md5sum);
    char cache_key[CACHE_KEY_SIZE];
    char save_path[512];
    char save_name[512];
    gen_key(cache_key, md5sum, 0);
    if(exist_cache(thr_arg, cache_key) == 1)
    {
        LOG_PRINT(LOG_DEBUG, "File Exist, Needn't Save.");
        result = 1;
        goto done;
    }
    LOG_PRINT(LOG_DEBUG, "exist_cache not found. Begin to Save File.");


    if(settings.mode != 1)
    {
        if(save_img_db(thr_arg, cache_key, buff, len) == -1)
        {
            LOG_PRINT(LOG_DEBUG, "save_img_db failed.");
            goto done;
        }
        else
        {
            LOG_PRINT(LOG_DEBUG, "save_img_db succ.");
            result = 1;
            goto done;
        }
    }

    //caculate 2-level path
    int lvl1 = str_hash(md5sum);
    int lvl2 = str_hash(md5sum + 3);

    snprintf(save_path, 512, "%s/%d/%d/%s", settings.img_path, lvl1, lvl2, md5sum);
    LOG_PRINT(LOG_DEBUG, "save_path: %s", save_path);

    if(is_dir(save_path) == 1)
    {
        LOG_PRINT(LOG_DEBUG, "Check File Exist. Needn't Save.");
        goto cache;
    }

    if(mk_dirs(save_path) == -1)
    {
        LOG_PRINT(LOG_DEBUG, "save_path[%s] Create Failed!", save_path);
        goto done;
    }
    LOG_PRINT(LOG_DEBUG, "save_path[%s] Create Finish.", save_path);
    snprintf(save_name, 512, "%s/0*0", save_path);
    LOG_PRINT(LOG_DEBUG, "save_name-->: %s", save_name);
    if(new_img(buff, len, save_name) == -1)
    {
        LOG_PRINT(LOG_DEBUG, "Save Image[%s] Failed!", save_name);
        goto done;
    }

cache:
    if(len < CACHE_MAX_SIZE)
    {
        gen_key(cache_key, md5sum, 0);
        set_cache_bin(thr_arg, cache_key, buff, len);
    }
    result = 1;

done:
    return result;
}
예제 #2
0
파일: zimg.c 프로젝트: Codefor/zimg
/**
 * @brief save_img Save buffer from POST requests
 *
 * @param buff The char * from POST request
 * @param len The length of buff
 * @param md5 Parsed md5 from url
 *
 * @return ZIMG_OK for success and ZIMG_ERR for fail
 */
int save_img(const char *buff, const int len, char *md5sum){
    if(buff == NULL || md5sum == NULL || len <=0){
	return ZIMG_ERR;
    }

    const char *image_format = get_img_format(buff);
    if(image_format == NULL){
	return ZIMG_ERR;
    }

    calc_md5sum(buff,len,md5sum);

    char cache_key[45];
    sprintf(cache_key, "img:%s:0:0:1:0", md5sum);

    if(exist_cache(cache_key) == 1){
	LOG_PRINT(LOG_INFO, "File Exist, Needn't Save.");
	return ZIMG_OK;
    }

    LOG_PRINT(LOG_INFO, "exist_cache not found. Begin to Check File.");

    char *save_path = (char *)malloc(512);
    if(save_path == NULL){
	return ZIMG_ERR;
    }
    //caculate 2-level path
    int lvl1 = str_hash(md5sum);
    int lvl2 = str_hash(md5sum + 3);

    sprintf(save_path, "%s/%d/%d/%s/0*0p", settings.img_path, lvl1, lvl2, md5sum);
    LOG_PRINT(LOG_INFO, "save_path: %s", save_path);

    if(is_file(save_path) == ZIMG_OK){
	LOG_PRINT(LOG_INFO, "Check File Exist. Needn't Save.");
	//cache
	if(len < CACHE_MAX_SIZE)
	{
	    // to gen cache_key like this: rsp_path-/926ee2f570dc50b2575e35a6712b08ce
	    set_cache_bin(cache_key, buff, len);
	}
	free(save_path);
	return ZIMG_OK;
    }else{
	char *p = strrchr(save_path,'/');
	*p = '\0';
	if(mk_dirs(save_path) == ZIMG_ERR){
	    LOG_PRINT(LOG_ERROR, "save_path[%s] Create Failed!", save_path);
	    free(save_path);
	    return ZIMG_ERR;
	}

	chdir(_init_path);
	LOG_PRINT(LOG_INFO, "save_path[%s] Create Finish.", save_path);

	*p = '/';
	LOG_PRINT(LOG_INFO, "save_name-->: %s", save_path);

	if(new_img(buff, len, save_path) != ZIMG_OK){
	    LOG_PRINT(LOG_WARNING, "Save Image[%s] Failed!", save_path);
	    free(save_path);
	    return ZIMG_ERR;
	}

	//shrink as JPEG
	p = strrchr(save_path,'/');
	memcpy(p+1,"0.jpg",5);
	*(p + 6) = '\0';

	if(convert2jpg(buff,len,save_path) == ZIMG_OK){
	    LOG_PRINT(LOG_WARNING, "Convert Image to JPEG [%s] OK!", save_path);
	}

	free(save_path);
	return ZIMG_OK;
    }
}