MCPcopy Create free account
hub / github.com/Moddable-OpenSource/moddable / downloadGIF

Method downloadGIF

contributed/giphy/main.js:71–133  ·  view source on GitHub ↗
(application, url)

Source from the content-addressed store, hash-verified

69 }
70 }
71 downloadGIF(application, url){
72 let ready = false;
73 globalThis.partition ??= new Flash("xs");
74 const blockSize = partition.blockSize;
75 partition.erase(0);
76 const buffer = partition.map();
77 let blockIndex = 1;
78 let offset = 0;
79 let pathIndex = url.indexOf("/media/");
80 let host = (url.indexOf("https") > -1)? url.slice(8, pathIndex) : url.slice(7, pathIndex);
81 let path = url.slice(pathIndex);
82 let reader;
83 let request = this.request = new Request({
84 host,
85 path,
86 port: 443,
87 Socket: SecureSocket,
88 secure: {protocolVersion: 0x303, verify: false }
89 })
90 request.callback = function(message, value, etc) {
91 switch (message) {
92 case Request.header:
93 if ("content-length" === value)
94 this.length = parseInt(etc, 10);
95 break;
96
97 case Request.responseFragment: {
98 const data = this.read(ArrayBuffer);
99 const byteLength = data.byteLength;
100 while ((offset + byteLength) > blockIndex * blockSize) {
101 partition.erase(blockIndex);
102 blockIndex++;
103 }
104 partition.write(offset, byteLength, data);
105 offset += byteLength;
106
107 application.distribute("onUpdateProgress", offset/this.length);
108 if (ready) break;
109
110 if (!reader) {
111 try {
112 reader = new ReadGIF(buffer, {available: offset});
113 } catch {
114 return;
115 }
116 }
117
118 reader.available = offset;
119 ready = reader.ready;
120 if (!ready) break;
121
122 application.first.defer("showFirstFrame");
123 reader = undefined;
124 } break;
125
126 case Request.responseComplete:
127 trace(`Transfer complete.\n`);
128 reader = undefined;

Callers

nothing calls this directly

Calls 7

eraseMethod · 0.80
mapMethod · 0.80
distributeMethod · 0.80
deferMethod · 0.80
sliceMethod · 0.65
readMethod · 0.65
writeMethod · 0.65

Tested by

no test coverage detected