Example #1
0
var _generateSizes = function(ctx, principal, x, y, width, callback) {
    // Retrieve the raw image.
    ContentUtil.getStorageBackend(ctx, principal.picture.largeUri).get(ctx, principal.picture.largeUri, function(err, file) {
        if (err) {
            return callback(err);
        }

        // Get the resized images
        var selectedArea = {
            'x': parseInt(x, 10),
            'y': parseInt(y, 10),
            'width': parseInt(width, 10),
            'height': parseInt(width, 10)
        };
        var sizes = [
            {'width': PrincipalsConstants.picture.size.SMALL, 'height': PrincipalsConstants.picture.size.SMALL},
            {'width': PrincipalsConstants.picture.size.MEDIUM, 'height': PrincipalsConstants.picture.size.MEDIUM}
        ];
        ImageUtil.cropAndResize(file.path, selectedArea, sizes, function(err, files) {
            // Remove the temp file first
            file.remove(function(removalError) {
                if (err) {
                    return callback(err);
                } else if (removalError) {
                    return callback(removalError);
                }

                // File removed, store and save the cropped and resized images
                _storeAndSave(ctx, principal, files, callback);
            });
        });
    });
};
Example #2
0
var _storeAndSave = function(ctx, principal, files, callback) {
    // Store the resized files.
    var backend = ContentUtil.getStorageBackend(ctx);

    // Get the the small image.
    var key = PrincipalsConstants.picture.size.SMALL + 'x' + PrincipalsConstants.picture.size.SMALL;
    var smallImage = files[key];

    // Store the image with a correct filename.
    // We explicitly add a correct extension as nginx uses it to determine the mimetype.
    var options = {
        'resourceId': principal.id,
        'prefix': 'profilepictures',
        'filename': 'small' + ImageUtil.getImageExtension(smallImage.filename, '.jpg')
    };
    backend.store(ctx, smallImage, options, function(err, smallPictureUri) {
        if (err) {
            return callback(err);
        }

        // Get the medium image, determine the correct extension and store it.
        key = PrincipalsConstants.picture.size.MEDIUM + 'x' + PrincipalsConstants.picture.size.MEDIUM;
        var mediumImage = files[key];
        options.filename = 'medium' + ImageUtil.getImageExtension(mediumImage.filename, '.jpg');
        backend.store(ctx, mediumImage, options, function(err, mediumPictureUri) {
            if (err) {
                return callback(err);
            }

            // Files stored, save them to the DB.
            _saveUrisToCassandra(ctx, principal, smallPictureUri, mediumPictureUri, callback);
        });
    });
};
Example #3
0
        ImageUtil.convertToJPG(orientedFile.path, function(err, convertedFile) {
            if (err) {
                return cleanupOnError(err, file, callback);
            }

            // Store the oriented file
            var options = {
                'resourceId': principal.id,
                'prefix': 'profilepictures',
                'filename': 'large.jpg'
            };
            ContentUtil.getStorageBackend(ctx).store(ctx, convertedFile, options, function(err, largePictureUri) {
                if (err) {
                    return cleanupOnError(err, convertedFile, callback);
                }

                // By this point the temp file has been removed from disk,
                // no need to clean up in error cases below.

                PrincipalsDAO.updatePrincipal(principal.id, {'largePictureUri': largePictureUri}, function(err) {
                    if (err) {
                        return callback(err);
                    }

                    principal.picture.largeUri = largePictureUri;
                    principal.picture.large = PrincipalsUtil.getSignedPictureUrl(ctx, largePictureUri);
                    return callback(null, principal);
                });
            });
        });
Example #4
0
const uploadLogoFile = function(ctx, file, tenantAlias, callback) {
  tenantAlias = tenantAlias || ctx.tenant().alias;
  if (!ctx.user() || !ctx.user().isAdmin(tenantAlias)) {
    return callback({ code: 401, msg: 'Only administrators can upload new logos for tenants' });
  }

  const extension = file.name.split('.').pop();
  if (!extension.match(/(gif|jpe?g|png)$/i)) {
    return callback({ code: 500, msg: 'File has an invalid mime type' });
  }

  const options = {
    prefix: util.format('logos/%s', tenantAlias)
  };
  ContentUtil.getStorageBackend(ctx).store(ctx.tenant().alias, file, options, (err, uri) => {
    if (err) {
      return callback(err);
    }

    const signedUrl = ContentUtil.getSignedDownloadUrl(ctx, uri, -1, -1);
    return callback(null, signedUrl);
  });
};