node.js - Is there a way to convert a base64 encoded string into 8-bit ASCII in NodeJS? -


i'm writing code in firebase functions acts intermediary between 2 databases. (i've been mandated way.)

i need send user data , pdfs on database 1 database 2. have working except pdfs. there pdfs right number of pages in database 2, problem pdfs show blank in user portal , when download them database.

database 1 sends me base64 encoded string pdf. don't know i'm supposed send database 2. database 2 has api , sparse documentation in ruby; provide examples, , there little support api. below part haven't been able figure out far:

post_body << "--#{boundary}\r\n"  post_body << "content-disposition: form-data;  name=\"document[attachments_attributes][0][attachment]\";  filename=\"document.pdf\"\r\n"  post_body << "content-type: application/pdf \r\n"  post_body << "\r\n"  post_body << file.read(file)  post_body << "\r\n--#{boundary}--\r\n" # last boundary prefixed , suffixed  --

the part don't know file.read(file). don't know how equivalent of whatever method returns in nodejs environment base64 string. tried file.read(file).encoding in irb , returned utf-8, tried buffer.from(base64string, 'base64').tostring('utf8'), didn't work.

i've tried every encoding pass in method ('ascii' (7-bit), 'utf8', 'utf16le', 'latin1', 'binary', 'hex' -- nodejs.org/api/buffer.html#buffer_class_method_buffer_from_array). none of them work. tried converting utf-8 string string of javascript escape sequences. tried this package. googled common encodings in ruby , saw 8-bit ascii 1 of them, i'm trying convert base64 8-bit ascii in nodejs.

if have idea of i'm not doing correctly, that'd great. or if don't have idea of how convert base64 8-bit ascii, that'd great too.

below snippet of file.read(file) returns in irb:

creator(շ\x8fc\\(t~\xc8\u0018\u0005\xa6\x8f\xb8\xd2\u0017\x8c\u0014\x87b\u0004\xe8\xb4ΐ<Έ\b)/producer(շ\x8fc\\(t~\xc8\f\\(\x93\xb6\xb0\xd5\u001e\x9bf\x90{\u001a\xf6\xad\xfb\xe8\xd0\xeb\xd9\u0005ŕ\ew\xf3)/moddate(\xd0\xee\xcf<z-:\xdexu\xd1\xf7\xed\x8ea\xc8\u0019\x80{\u0013\xf6\xb3\xe9)/company(ӻ\x8bi8{g\x8d&5\xc0\xad\xbf\x99+\x8b_\xdf\")/sourcemodified(\xd0\xee\xcf<z-:\xdexu\xd2\xf0\xed\x8bg\xc8)/title(\xb4\x84\xb9jjao\x9b<a\xb0\xa3\xbe\xdc)>>\rendobj\rxref\r\n0 37\r\n0000000000 65535 f\r\n0000014115 00000 n\r\n0000014965 00000 n\r\n0000014998 00000 n\r\n0000015021 00000 n\r\n0000015072 00000 n\r\n0000019484 00000 n\r\n0000000000 65535 f\r\n0000000000 65535 f\r\n0000000000 65535 f\r\n0000000000 65535 f\r\n0000000000 65535 f\r\n0000000000 65535 f\r\n0000000000 65535 f\r\n0000000000 65535 f\r\n0000000000 65535 f\r\n0000000000 65535 f\r\n0000000000 65535 f\r\n0000000000 65535 f\r\n0000000000 65535 f\r\n0000000000 65535 f\r\n0000000000 65535 f\r\n0000000000 65535 f\r\n0000000000 65535 f\r\n0000000000 65535 f\r\n0000000000 65535 f\r\n0000000000 65535 f\r\n0000000000 65535 f\r\n0000000000 65535 f\r\n0000000000 65535 f\r\n0000000000 65535 f\r\n0000000000 65535 f\r\n0000000000 65535 f\r\n0000000000 65535 f\r\n0000000000 65535 f\r\n0000000000 65535 f\r\n0000000000 65535 

it looks ascii characters escape sequences me.

below more of example, in case need it:

#!/usr/bin/env ruby  require "net/http"  require "time"  require "digest"  require "openssl"  require "base64"    # token used separate each content part. string should  # random string , not appear in file body.    boundary = "multipartboundary"  uri = uri.parse "https://kipuapi.kipuworks.com/api/patients"  file = "spec/fixtures/files/document.pdf"    post_body = ""    post_body << "--#{boundary}\r\n" # boundaries prefixed --  post_body << "content-disposition: form-data;  name=\"document[data][first_name]\"\r\n"  post_body << "\r\n"  post_body << "john"  post_body << "\r\n"    post_body << "--#{boundary}\r\n" # boundary's prefixed --  post_body << "content-disposition: form-data;  name=\"document[data][last_name]\"\r\n"  post_body << "\r\n"  post_body << "smith"  post_body << "\r\n"    post_body << "--#{boundary}\r\n"  post_body << "content-disposition: form-data; name=\"document[recipient_id]\"\r\n"  post_body << "\r\n"  post_body << @access_id # sending ourselves  post_body << "\r\n"    post_body << "--#{boundary}\r\n"  post_body << "content-disposition: form-data; name=\"app_id\"\r\n"  post_body << "\r\n"  post_body << @access_id  post_body << "\r\n"    post_body << "--#{boundary}\r\n"  post_body << "content-disposition: form-data;  name=\"document[attachments_attributes][0][attachment]\";  filename=\"document.pdf\"\r\n"  post_body << "content-type: application/pdf \r\n"  post_body << "\r\n"  post_body << file.read(file)  post_body << "\r\n--#{boundary}--\r\n" # last boundary prefixed , suffixed  --    http = net::http.new(uri.host, uri.port)  request = net::http::post.new(uri.request_uri)  request.body = post_body  request["content-type"] = "multipart/form-data, boundary=#{boundary}"  request["accept"] = "application/vnd.kipusystems+json; version=1"  request["date"] = time.now.httpdate  request["content-md5"] = digest::md5.base64digest request.body  canonical_string = [   request["content-type"],   request["content-md5"],   uri.request_uri,   request["date"]  ].join ","  digest = openssl::digest.new "sha1"  signed = openssl::hmac.digest digest, @secret_key, canonical_string  encoded_sig = base64.strict_encode64 signed  19 / 23  request["authorization"] = "apiauth #{@access_id}:#{encoded_sig}"  response = http.request request  puts response.body

here part of relevant code in firebase (note @ time, wrote 1 function handle , post requests thinking going small project, regret),

function createreq(uri, body, credobj, method, multipart, res){      if (multipart){          let postbody = '';          let dataobj = body.document.data;          let attachmentsarr = body.document['attachment_attributes'];    // formatting data in dataobj, appending postbody            (let = 0; < attachmentsarr.length; i++){              postbody += '--'+boundary+'\r\n';              postbody += 'content-disposition: form-data; ';              postbody += 'name="document[attachments_attributes]['+ +'][attachment]'+'"; ';              postbody += 'filename="'+ attachmentsarr[i]['filename'] + '"\r\n';              postbody += 'content-type: ' + attachmentsarr[i]['content_type'] +' \r\n';              postbody += '\r\n';                // todo: need base64 attachment string here                             //console.log(attachmentsarr[i]['attachment']);                if (i === attachmentsarr.length - 1){                  postbody += '\r\n--'+boundary+'--\r\n';                }          } // closes attachmentarr loop    // authentication code, define variables signature , contentstr            let reqobj = {              url: root_url+uri,              method: 'post',              headers: {                  'content-type': 'multipart/form-data, boundary='+boundary,                  'accept': accept_header_value,                  'authorization': 'apiauth '+credobj.accessid+':'+signature,                  'date': new date().toutcstring(),                  'content-md5': contentstr              },              body: postbody          };            return reqobj;        } else // other code non-multipart post requests  }

the returned reqobj gets sent database 2 in promise: (i've commented out code sends request database 2 now)

...    }).then(reqobj => {    // code method  // ...     else if ('post' === method){      reqobj.method = 'post';              console.log('this request obj inside last promise');              console.log(reqobj);          //     request.post(reqobj, function(error, response, body){          //         if (!error){          //             console.log('this response kipu');          //             console.log(body);          //             res.send(body);          //         } else {          //             console.log(error);          //             res.send(error);          //         }          //     });          }      }).catch(err => {          res.send(err);      });


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -