fossasia/loklak_webclient

View on GitHub
app/js/services/file.js

Summary

Maintainability
A
35 mins
Test Coverage
'use strict';

var servicesModule = require('./_index.js');

/**
 * @ngInject
 */
function FileService() {

  var service = {};

  service.Base64StrToBlobStr = function(base64str,contentType, sliceSize) {
    contentType = contentType || '';
    sliceSize = sliceSize || 512;

    var byteCharacters = atob(base64str);
    var byteArrays = [];

    for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
        var slice = byteCharacters.slice(offset, offset + sliceSize);

        var byteNumbers = new Array(slice.length);
        for (var i = 0; i < slice.length; i++) {
            byteNumbers[i] = slice.charCodeAt(i);
        }

        var byteArray = new Uint8Array(byteNumbers);

        byteArrays.push(byteArray);
    }

    var blob = new Blob(byteArrays, {type: contentType});
    return blob;    
  };

  return service;

}

servicesModule.service('FileService', FileService);