1

I am receiving a bunch of images together in base64 format in a HttpResponse. I am querying my java heroku app which returns a response something like this:

[imgData]{base64image}[/imgData]/n [imgData]{base64image}[/imgData]

I add the [imgData] and [/imgData] tags in my java class to separate the base64 image data from each other and the new line character separate each image.

In my apex class I am doing something like this:

        String images = response.getBody();
        String[] imageArray = image.split('/n');
        Integer imageCount = 0;
        List<String> base64Values = new List<String>();
        for(String base64Img : imageArray) {
            base64Values.add(base64Img.subStringBetween('[imgData]', '[/imgData]').trim());
        }

I need to use this list and generate attachments from these images.

Now the base64 value of a single image is pretty large, so large that I cant even paste here to show you guys.

Now my problem is when my apex class runs, I expect it to split the large string and then just save the base64 encoded value in the list.

But it gives me System.LimitException: Regex too complicated error.

Any idea on how I can extract the base64 from that large response? Or should I just change the way I am sending the values to the apex class from heroku?

2 Answers 2

1

You've got up to 6M bytes of heap to play with (and I think a maximum HTTP body size of 3M bytes) so if say the base64 version of all the images is always smaller than say a couple of Mbytes then sending them all at once should be OK.

String has an indexOf method that you could use to work through each block of base64 in turn first finding a '[imgData]' and then the following '[/imgData]' and using substring you can pull out the text in between without hitting the regex problem.

0
1

Using the method that Keith C suggested.

        String media = '[img]john[/img][img]jill[/img][img]jack[/img][img]judy[/img][img]jamie[/img][img]james[/img][img]johnny[/img]';
        Integer first = 0; 
        Integer last = 0;
        Integer lastOccurrence = media.lastIndexOf('[/img]');
        while(true) {
            first = media.indexOf('[img]', last);
            last = media.indexOf('[/img]', first);
            System.debug('first : ' + first);
            System.debug('last : ' + last);
            String substring = media.substring(first + 5, last);
            System.debug('substring : ' + substring);
            if(last == lastOccurrence) {
                break;
            }
        }

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .