MCPcopy Create free account
hub / github.com/tensorflow/tfjs-examples / fetchOnceAndSaveToDiskWithBuffer

Function fetchOnceAndSaveToDiskWithBuffer

quantization/data_mnist.js:48–78  ·  view source on GitHub ↗
(
    baseURL, destDir, filename)

Source from the content-addressed store, hash-verified

46
47// Downloads a test file only once and returns the buffer for the file.
48export async function fetchOnceAndSaveToDiskWithBuffer(
49 baseURL, destDir, filename) {
50
51 return new Promise(async (resolve, reject) => {
52 const url = `${baseURL}${filename}.gz`;
53 const localPath = path.join(destDir, filename);
54 if (await exists(localPath)) {
55 resolve(readFile(localPath));
56 return;
57 }
58 const file = fs.createWriteStream(filename);
59 console.log(` * Downloading from: ${url}`);
60 let httpModule;
61 if (url.indexOf('https://') === 0) {
62 httpModule = https;
63 } else if (url.indexOf('http://') === 0) {
64 httpModule = http;
65 } else {
66 return reject(`Unrecognized protocol in URL: ${url}`);
67 }
68
69 httpModule.get(url, (response) => {
70 const unzip = zlib.createGunzip();
71 response.pipe(unzip).pipe(file);
72 unzip.on('end', async () => {
73 await rename(filename, localPath);
74 resolve(readFile(localPath));
75 });
76 });
77 });
78}
79
80function loadHeaderValues(buffer, headerLength) {
81 const headerValues = [];

Callers 2

loadImagesFunction · 0.70
loadLabelsFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected