Skip to content

Commit

Permalink
Support ZIP Stream
Browse files Browse the repository at this point in the history
  • Loading branch information
jaruba committed Mar 12, 2024
1 parent b49b87d commit 6b8290e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/withStreamingServer/convertStream.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var magnet = require('magnet-uri');
var createTorrent = require('./createTorrent');
var createRar = require('./createRar');
var createZip = require('./createZip');

function convertStream(streamingServerURL, stream, seriesInfo) {
return new Promise(function(resolve, reject) {
Expand Down Expand Up @@ -61,6 +62,18 @@ function convertStream(streamingServerURL, stream, seriesInfo) {
return;
}

if (stream.zipUrls && Array.isArray(stream.zipUrls)) {
createZip(streamingServerURL, stream.zipUrls, stream.fileIdx, stream.fileMustInclude)
.then(function(zipStream) {
resolve({ url: zipStream.url, fileIdx: zipStream.fileIdx, fileMustInclude: zipStream.fileMustInclude });
})
.catch(function(error) {
reject(error);
});

return;
}

reject(new Error('Stream cannot be converted'));
});
}
Expand Down
43 changes: 43 additions & 0 deletions src/withStreamingServer/createZip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
var url = require('url');

function buildZipStream(streamingServerURL, key, fileIdx, fileMustInclude) {
var opts = {};
if (fileIdx) {
opts.fileIdx = fileIdx;
}
if (fileMustInclude) {
opts.fileMustInclude = fileMustInclude;
}
return {
url: url.resolve(streamingServerURL, '/zip/stream?key=' + encodeURIComponent(key) + (Object.keys(opts).length ? 'o=' + encodeURIComponent(JSON.stringify(opts)) : '')),
fileIdx: fileIdx,
fileMustInclude: fileMustInclude
};
}

function createZip(streamingServerURL, zipUrls, fileIdx, fileMustInclude) {
if (!(zipUrls || []).length) {
return Promise.reject('No ZIP URLs provided');
}

return fetch(url.resolve(streamingServerURL, '/zip/create'), {
method: 'POST',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify(zipUrls)
}).then(function(resp) {
if (resp.ok) {
return resp.json();
}

throw new Error(resp.status + ' (' + resp.statusText + ')');
}).then(function(resp) {
if (!resp.key) {
throw new Error('Could not retrieve ZIP stream key');
}
return buildZipStream(streamingServerURL, resp.key, fileIdx, fileMustInclude);
});
}

module.exports = createZip;

0 comments on commit 6b8290e

Please sign in to comment.