简体   繁体   English

如何根据请求散列和下载图像?

[英]How to hash and download images with request?

Situation 情况

What I want to achieve, is download an image to a buffer, hash it, then save it. 我要实现的是将图像下载到缓冲区,对其进行哈希处理, 然后保存。

I can not write to disk before hashing. 我无法哈希之前写入磁盘。

Code (inside a Promise) 代码(在Promise中)

const filename = this.getPathFromHash(hash);
if (!fs.existsSync(path.dirname(filename))) fs.mkdirSync(path.dirname(filename));
fs.writeFile(filename, body, 'binary', (e) => {
  if (e) reject(e);
  parser.MediumVersion.create({
    hash, url, type, size, width, height,
  }).then(resolve).catch(reject);
});

Result 结果

file corruption, but it created the files :) 文件损坏,但它创建了文件:)

Edit: IrFanView says that it's JPEG with an incorrect extension, then it just says not a jpeg file, starts with (two random bytes here) 编辑:IrFanView说它是具有不正确扩展名的JPEG,然后它只是说不是 jpeg文件,以(此处为两个随机字节)开头

Edit2: What is this?? Edit2:这是什么? Four 0xfd bytes? 四个0xfd字节? What do they mean? 他们的意思是什么? Otherwise the image header looks OK. 否则,图像标题看起来不错。

fdfdfdfd

Edit3: Removing those bytes do not solve the problem :/ Maybe encoding is the culprit? Edit3:删除这些字节不能解决问题:/也许是罪魁祸首? I've tried changing it to UTF-8 , no luck so far. 我已经尝试将其更改为UTF-8 ,到目前为止还没有运气。

Edit4: Quick comparison! Edit4:快速比较! 1st one downloaded using my program, second one, Chrome. 第一个使用我的程序下载,第二个使用Chrome。

比较

Directory structure 目录结构

I know, I know, SHA-256 hashes are 64 characters long. 我知道,SHA-256散列的长度为64个字符。 For the sake of simplicity, I'm going to use 8 characters. 为了简单起见,我将使用8个字符。

Given the hash 0123abcd , create the directory 01 (if it doesn't exist ( slice(0,2) ), then put the file with name 23abcd ( slice(2) ) in it. I've seen this structure in lots of caches. 给定哈希0123abcd ,创建目录01 (如果它不存在( slice(0,2) ),然后将名称为23abcdslice(2) )的文件放入其中。缓存。

Example: 例:

01/
  23abcd
  45abcd
ab/
  cdef01
  010101

As per https://github.com/request/request#requestoptions-callback : 根据https://github.com/request/request#requestoptions-callback

if you expect binary data, you should set encoding: null . 如果需要二进制数据,则应设置encoding: null

So for me that would be request(url, { encoding: null }, (e, resp, body) => {...}) . 所以对我来说,这就是request(url, { encoding: null }, (e, resp, body) => {...})

In that case, the image is perfectly fine and readable :) 在那种情况下,图像是完美的并且可读的:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM