1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package Torello.HTML.Tools.Images;

import Torello.Java.StrPrint;
import Torello.Java.Additional.Ret2;

import java.awt.image.BufferedImage;

class MainLoopBody 
{
    // Helps "FIND" the bugs.  There are only 6 extra boolean-comparisons for a println
    // There is no need to delete this right now.

    private static final boolean DEBUGGING = false;

    static void loop(final RECORD r) throws ImageScraperException
    {
        // Print URL-Iterable Number (request.counterPrinter)
        if (r.logLevelEQ1)
            r.append(r.request.counterPrinter.apply(r.results.pos) + ": ");

        if (r.logLevelGTEQ2)
            r.append("\n" + r.request.counterPrinter.apply(r.results.pos) + ": ");

        if (DEBUGGING) System.out.println("HERE: 01 (" + r.results.pos + ")");


        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        // DECIDE: Which of the three cases this is: URL, B64-Image, or an Exception-URL
        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***

        // If there was an Image-URL next, then print it !!!
        if (r.url != null)
            { if (r.logLevelGTEQ2) r.append("Image-URL: [" + r.url.toString() + "]\n"); }

        // If There was no URL, Then this is likely a B64-Encoded Image
        else if ((r.b64ImageData = r.request.nextB64Image()) != null)
        {
            r.isB64EncodedImage = true;

            if (r.logLevelGTEQ2) r.append(
                "BASE-64 IMAGE: " + r.b64ImageData[0] /* imageFormatStr */ + ',' +
                StrPrint.abbrev(r.b64ImageData[1], 35, true, " ... ", 70) + '\n'
            );
        }

        // If url is null, and this isn't a "B64-Encoded", then it's an Exception-Throw URL
        else
            { r.dealWithExceptionURL();  return; }


        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        // DOWNLOAD & CONVERT
        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***

        if (DEBUGGING) System.out.println("HERE: 02 (" + r.results.pos + ")");

        // If the user provided a 'urlPreProcessor' in his Request-instance, run that now.
        r.doUserURLPreProcessorIfNeeded();

        if (DEBUGGING) System.out.println("HERE: 03 (" + r.results.pos + ")");

        // Get the java.awt.image.BufferedImage instance
        Ret2<BufferedImage, IF> ret2BufferedImage = r.isB64EncodedImage
            ? ConvertB64Image.run(r)
            : DownloadImage.run(r);

        if (DEBUGGING) System.out.println("HERE: 04 (" + r.results.pos + ")");

        // If 'null' is returned, The User Requested 'skipOn...' SO - skip-and-move-on.
        //    * Log-Messages will ALREADY have been printed
        //    * class Results array's will ALREADY have been updated. 
        //    * If an ImageScraperException is needed, it would ALREADY have been thrown.

        if (ret2BufferedImage == null) return;

        // Convert java.awt.image.BufferedImage into a byte[]-Array
        // This 'r2' contains the Image as a byte[]-Array, and the format in which it was saved

        Ret2<byte[], IF> ret2ByteArrImage = BufferedImageToByteArray.convert
            (r, ret2BufferedImage.a /* The Image */, ret2BufferedImage.b /* The Extension */);

        if (DEBUGGING) System.out.println("HERE: 05 (" + r.results.pos + ")");

        // SAME AS PREVIOUS if (...) return;
        if (ret2ByteArrImage == null) return;


        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        // SAVE THE IMAGE (or send to 'Request.imageReceiver')
        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***

        // Now Build an "ImageInfo" instance. (This is sent to any/all User's Lambdas)
        // NOTE: No error-checking needed for a class that is strictly a data / "RECORD" class

        r.imageInfo = new ImageInfo(
            // Image-URL (very common)
            r.url,

            // Base-64 Image Stuff (rare, but not impossible)
            r.isB64EncodedImage,
            (r.isB64EncodedImage ? r.b64ImageData : null),

            // The actual downloaded and converted images, themselves
            ret2BufferedImage.a,    // java.awt.image.BufferredImage
            ret2ByteArrImage.a,     // byte[] imgByteArr

            // URL-Aquired Extension & Decided-Upon Extension
            ret2BufferedImage.b,    // guessedExt
            ret2ByteArrImage.b,     // actualExt

            // Results Array Counters
            r.results.pos,
            r.results.successCounter
        );

        if (DEBUGGING) System.out.println("HERE: 06 (" + r.results.pos + ")");

        // Save to Disk, or Send to Request.imageReceiver
        r.handleImageByteArray();
    }
}