40 lines
1.4 KiB
JavaScript
40 lines
1.4 KiB
JavaScript
'use strict';
|
|
|
|
const fs = require('fs-extra');
|
|
const path = require('path');
|
|
const config = require('config');
|
|
|
|
const fetchItem = require('./item.js');
|
|
const interpolate = require('../interpolate.js');
|
|
const save = require('../save/save.js');
|
|
const textToStream = require('../save/textToStream.js');
|
|
|
|
module.exports = function(posts, user) {
|
|
return Promise.all(posts.map(post => {
|
|
return Promise.resolve().then(() => {
|
|
return Promise.all(post.content.items.map((item, index) => {
|
|
item.index = index;
|
|
|
|
if(item.self) {
|
|
return Object.assign({}, item, {stream: textToStream(item.text)});
|
|
}
|
|
|
|
return fetchItem(item.url, 0).then(stream => {
|
|
return Object.assign({}, item, {stream});
|
|
});
|
|
}));
|
|
}).then(items => {
|
|
return Promise.all(items.map(item => {
|
|
const type = item.type.split('/')[0];
|
|
const filepath = post.content.album ? interpolate(config.library.album[type], user, post, item) : interpolate(config.library[type], user, post, item);
|
|
|
|
return Promise.resolve().then(() => {
|
|
return fs.ensureDir(path.dirname(filepath));
|
|
}).then(() => {
|
|
return save(filepath, item.stream)
|
|
});
|
|
}));
|
|
});
|
|
}));
|
|
};
|