-3

I have a list of int representing the hexa code or int8 of a picture, that I need to insert to MySQL as Blob. I am using a NodeJS Express server to do this, in an API. I am using the node packet node-mysql2, my NodeJS version is NodeJS 12.

So my API receive this : [137,80,78,71,13,10,26,10,0,0,0,...96,130]

I want to insert this as Blob in MYSQL (this is a picture). How can I do this please ? I didn't found anything... I've done a lot of StackOverflow subjects and Google pages but I did not found.

Thanks by advance for the help !

tadman
  • 208,517
  • 23
  • 234
  • 262
Martin S.
  • 21
  • 7
  • every number represent a byte so convert all anf fill uo a byte array – nbk Feb 19 '21 at 00:10
  • Please, **trim this thing down to a sensible size**. We don't need to scroll past all of this random junk. Show us **what you tried** not whatever this is. – tadman Feb 19 '21 at 01:11
  • A) Why aren't you receiving this as binary data, or at the absolute least, base64 encoded? This encoding is completely absurd. B) Why do you want to save this in a database? That's the last place images should be. – tadman Feb 19 '21 at 01:12
  • @nbk Okay, I'll try, thank you ! – Martin S. Feb 19 '21 at 01:27
  • 1
    @tadman Oh... Sorry... I didn't knew. I am a beginner, learning so I can't really answer your questions.. For the A), I receive this from an application that an other developer created. I don't have any access to the code of this app. For the B, I don't know, this is my lead developer who choose to store it like this. – Martin S. Feb 19 '21 at 01:29
  • That developer is just making your life miserable for no reason. You'll probably need to convert this into a `Buffer` and then write that to a `BLOB` field of suitable size, though I would test writing to regular files first since those are a lot easier to test, you can just open them up in a browser, for example, to see if you're getting valid image data. – tadman Feb 19 '21 at 01:30
  • @tadman About your first comment, I tried creating a Buffer from it, to be able to pass the buffer to MySQL. I also tried to convert it as binary file, as hexa code, as Blob with some npm packets, but anything worked, it always create well an entry in MySQL but the blob is always looking corrupted.. – Martin S. Feb 19 '21 at 01:32
  • 1
    @tadman Okay, thank you ! I'll continue looking for `Buffer` so. And I'll try to do as you tell. Thanks for the help ! – Martin S. Feb 19 '21 at 01:34
  • I'd recommend using a database adapter that's a bit higher level than the `mysql` driver, like [Sequelize](https://sequelize.org/master/). It has tools for dealing with abstractions like Buffer, etc. – tadman Feb 19 '21 at 01:34
  • Ohhh ok, seems interesting. I'll look about this ! Thanks a lot ! – Martin S. Feb 19 '21 at 01:34
  • To give you an idea of how inefficient that encoding is, the raw image data is 3,971 bytes, while the encoded form is 13,637 bytes, or in other words, 3.4x the original size. Base64 by comparison is only 1.5x bigger. Hex-dumped is 2x bigger and would *still* be more efficient. – tadman Feb 19 '21 at 01:37
  • Wow, okay, I understand... I do agree this is strange. I'll ask tomorrow why he does this... Also, with some more searches, it seems this format is like a `Uint8List` in his `Flutter` app, so I am trying to handle it. I already tried a `Buffer` from the `Uint8List` but it does not work too. – Martin S. Feb 19 '21 at 01:55

2 Answers2

-1

Assuming this data is valid, you should be able to convert it back quite easily:

let img = Buffer.from(JSON.parse(input));

Where input is that bizarre representation of your image that, coincidentally perhaps, is valid JSON.

tadman
  • 208,517
  • 23
  • 234
  • 262
  • Well, I really thank you for the help. But it didn't worked. I already tried a thing like this before, but I tried again with your answer to be sure. But this is not working, I still see my file in MySQL like if it was corrupted. But I'm sure this is an entire and valid file, because I take the picture from the app myself (I use a build of the app in a dev phone of my company). Maybe I should ask tomorrow to my lead dev why we put it in the database and how does he think to handle this.. – Martin S. Feb 19 '21 at 01:50
  • A) Does this decode properly when saved on disk? B) Can you put a known-good file into MySQL and get it back? C) How are you inserting? Are you using placeholder values? – tadman Feb 19 '21 at 02:01
  • A) Well... I tried to write it, and... No, it does not open. So it seems there is a problem in the app, maybe the other dev is manipulating in a bad way the picture before sending in to my API. I'll see tomorrow at work with him. Thanks anyway for the help ! And sorry for the time you waste. Anyway I learned a lot with all you said to me. – Martin S. Feb 19 '21 at 02:12
  • B) Yes, it worked well. But, I inserted it directly from PHPMyAdmin so I think the file is getting handled differently. C) I'm inserting with a simple SQL query, and I don't use placeholders – Martin S. Feb 19 '21 at 02:13
  • You should *always* use placeholder values. If you don't that's when bugs like this happen, or worse, SQL injection holes. – tadman Feb 19 '21 at 02:31
  • 1
    Okay, understand ! I'll do it now :) – Martin S. Feb 19 '21 at 02:36
-1

The answer is: Don’t do that! You should store a reference to a blob storage entry where you can save the blob data.

peni4142
  • 426
  • 1
  • 7
  • 26