/** * @brief get_img_mode_db Get image from db mode backend. * * @param req zimg request struct * @param buff_ptr buff pointer * @param img_size buff size * * @return 1 for success and -1 for failed */ int get_img_mode_db(zimg_req_t *req, evhtp_request_t *request) { int result = -1; char cache_key[CACHE_KEY_SIZE]; char *img_format = NULL; char *buff_ptr = NULL; size_t img_size; MagickBooleanType status; MagickWand *magick_wand = NULL; bool got_rsp = true; bool got_color = false; LOG_PRINT(LOG_DEBUG, "get_img() start processing zimg request..."); if(req->gray == 1) { gen_key(cache_key, req->md5, 4, req->width, req->height, req->proportion, req->gray); } else { if(req->proportion == 0 && req->width == 0 && req->height == 0) { gen_key(cache_key, req->md5, 0); } else { gen_key(cache_key, req->md5, 3, req->width, req->height, req->proportion); } } if(find_cache_bin(req->thr_arg, cache_key, &buff_ptr, &img_size) == 1) { LOG_PRINT(LOG_DEBUG, "Hit Cache[Key: %s].", cache_key); goto done; } LOG_PRINT(LOG_DEBUG, "Start to Find the Image..."); if(get_img_db(req->thr_arg, cache_key, &buff_ptr, &img_size) == 1) { LOG_PRINT(LOG_DEBUG, "Get image [%s] from backend db succ.", cache_key); if(img_size < CACHE_MAX_SIZE) { set_cache_bin(req->thr_arg, cache_key, buff_ptr, img_size); } goto done; } magick_wand = NewMagickWand(); got_rsp = false; if(req->gray == 1) { gen_key(cache_key, req->md5, 3, req->width, req->height, req->proportion); if(find_cache_bin(req->thr_arg, cache_key, &buff_ptr, &img_size) == 1) { LOG_PRINT(LOG_DEBUG, "Hit Color Image Cache[Key: %s, len: %d].", cache_key, img_size); status = MagickReadImageBlob(magick_wand, buff_ptr, img_size); free(buff_ptr); buff_ptr = NULL; if(status == MagickFalse) { LOG_PRINT(LOG_DEBUG, "Color Image Cache[Key: %s] is Bad. Remove.", cache_key); del_cache(req->thr_arg, cache_key); } else { got_color = true; LOG_PRINT(LOG_DEBUG, "Read Image from Color Image Cache[Key: %s, len: %d] Succ. Goto Convert.", cache_key, img_size); goto convert; } } if(get_img_db(req->thr_arg, cache_key, &buff_ptr, &img_size) == 1) { LOG_PRINT(LOG_DEBUG, "Get color image [%s] from backend db.", cache_key); status = MagickReadImageBlob(magick_wand, buff_ptr, img_size); if(status == MagickTrue) { got_color = true; LOG_PRINT(LOG_DEBUG, "Read Image from Color Image[%s] Succ. Goto Convert.", cache_key); if(img_size < CACHE_MAX_SIZE) { set_cache_bin(req->thr_arg, cache_key, buff_ptr, img_size); } free(buff_ptr); buff_ptr = NULL; goto convert; } } } gen_key(cache_key, req->md5, 0); if(find_cache_bin(req->thr_arg, cache_key, &buff_ptr, &img_size) == 1) { LOG_PRINT(LOG_DEBUG, "Hit Cache[Key: %s].", cache_key); } else { if(get_img_db(req->thr_arg, cache_key, &buff_ptr, &img_size) == -1) { LOG_PRINT(LOG_DEBUG, "Get image [%s] from backend db failed.", cache_key); goto err; } else if(img_size < CACHE_MAX_SIZE) { set_cache_bin(req->thr_arg, cache_key, buff_ptr, img_size); } } status = MagickReadImageBlob(magick_wand, buff_ptr, img_size); free(buff_ptr); buff_ptr = NULL; if(status == MagickFalse) { ThrowWandException(magick_wand); del_cache(req->thr_arg, cache_key); LOG_PRINT(LOG_DEBUG, "Read image [%s] from blob failed.", cache_key); goto err; } if(req->width == 0 && req->height == 0) { LOG_PRINT(LOG_DEBUG, "Image [%s] needn't resize. Goto Convert.", cache_key); goto convert; } int width, height; width = req->width; height = req->height; float owidth = MagickGetImageWidth(magick_wand); float oheight = MagickGetImageHeight(magick_wand); if(width <= owidth && height <= oheight) { if(req->proportion == 1 || (req->proportion == 0 && req->width * req->height == 0)) { if(req->height == 0) { height = width * oheight / owidth; } else { width = height * owidth / oheight; } } status = MagickResizeImage(magick_wand, width, height, LanczosFilter, 1.0); if(status == MagickFalse) { LOG_PRINT(LOG_DEBUG, "Image[%s] Resize Failed!", cache_key); goto err; } LOG_PRINT(LOG_DEBUG, "Resize img succ."); } else { got_rsp = true; LOG_PRINT(LOG_DEBUG, "Args width/height is bigger than real size, return original image."); } convert: //compress image if(got_color == false) { LOG_PRINT(LOG_DEBUG, "Start to Compress the Image!"); img_format = MagickGetImageFormat(magick_wand); LOG_PRINT(LOG_DEBUG, "Image Format is %s", img_format); if(strcmp(img_format, "JPEG") != 0) { LOG_PRINT(LOG_DEBUG, "Convert Image Format from %s to JPEG.", img_format); status = MagickSetImageFormat(magick_wand, "JPEG"); if(status == MagickFalse) { LOG_PRINT(LOG_DEBUG, "Image[%s] Convert Format Failed!", cache_key); } LOG_PRINT(LOG_DEBUG, "Compress Image with JPEGCompression"); status = MagickSetImageCompression(magick_wand, JPEGCompression); if(status == MagickFalse) { LOG_PRINT(LOG_DEBUG, "Image[%s] Compression Failed!", cache_key); } } size_t quality = MagickGetImageCompressionQuality(magick_wand); LOG_PRINT(LOG_DEBUG, "Image Compression Quality is %u.", quality); if(quality > WAP_QUALITY) { quality = WAP_QUALITY; } LOG_PRINT(LOG_DEBUG, "Set Compression Quality to 75%."); status = MagickSetImageCompressionQuality(magick_wand, quality); if(status == MagickFalse) { LOG_PRINT(LOG_DEBUG, "Set Compression Quality Failed!"); } //strip image EXIF infomation LOG_PRINT(LOG_DEBUG, "Start to Remove Exif Infomation of the Image..."); status = MagickStripImage(magick_wand); if(status == MagickFalse) { LOG_PRINT(LOG_DEBUG, "Remove Exif Infomation of the ImageFailed!"); } buff_ptr = (char *)MagickGetImageBlob(magick_wand, &img_size); if(buff_ptr == NULL) { LOG_PRINT(LOG_DEBUG, "Magick Get Image Blob Failed!"); goto err; } gen_key(cache_key, req->md5, 3, req->width, req->height, req->proportion); if(got_rsp == false) { save_img_db(req->thr_arg, cache_key, buff_ptr, img_size); } if(img_size < CACHE_MAX_SIZE) { set_cache_bin(req->thr_arg, cache_key, buff_ptr, img_size); } if(req->gray == 1) { free(buff_ptr); buff_ptr = NULL; } else { goto done; } } //gray image if(req->gray == 1) { LOG_PRINT(LOG_DEBUG, "Start to Remove Color!"); status = MagickSetImageColorspace(magick_wand, GRAYColorspace); if(status == MagickFalse) { LOG_PRINT(LOG_DEBUG, "Image[%s] Remove Color Failed!", cache_key); goto err; } LOG_PRINT(LOG_DEBUG, "Image Remove Color Finish!"); buff_ptr = (char *)MagickGetImageBlob(magick_wand, &img_size); if(buff_ptr == NULL) { LOG_PRINT(LOG_DEBUG, "Magick Get Image Blob Failed!"); goto err; } gen_key(cache_key, req->md5, 4, req->width, req->height, req->proportion, req->gray); if(got_rsp == false) { save_img_db(req->thr_arg, cache_key, buff_ptr, img_size); } if(img_size < CACHE_MAX_SIZE) { set_cache_bin(req->thr_arg, cache_key, buff_ptr, img_size); } } done: result = evbuffer_add(request->buffer_out, buff_ptr, img_size); if(result != -1) { result = 1; } err: if(magick_wand) { magick_wand=DestroyMagickWand(magick_wand); } if(img_format) free(img_format); if(buff_ptr) free(buff_ptr); return result; }
/** * @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; }