tanam
Version:
Pluggable CMS for Firebase
180 lines • 7.85 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const admin = require("firebase-admin");
const siteInfoService = require("../services/site-info.service");
const siteCollection = () => admin.firestore().collection('tanam').doc(process.env.GCLOUD_PROJECT);
function getFavicon() {
return __awaiter(this, void 0, void 0, function* () {
const contentFile = yield admin.storage().bucket().file('/tanam/favicon.ico');
const [contentExists] = yield contentFile.exists();
if (!contentExists) {
return null;
}
const [fileContent] = yield contentFile.download();
return fileContent;
});
}
exports.getFavicon = getFavicon;
function geThemeAssetsFile(filePath) {
return __awaiter(this, void 0, void 0, function* () {
console.log(`[geThemeAssetsFile] ${JSON.stringify({ filePath })}`);
const siteInfo = yield siteInfoService.getSiteInfo();
const fileQuery = yield siteCollection()
.collection('themes').doc(siteInfo.theme)
.collection('assets').where('title', '==', filePath)
.limit(1)
.get();
if (fileQuery.docs.length === 0) {
return null;
}
return fileQuery.docs[0].data();
});
}
exports.geThemeAssetsFile = geThemeAssetsFile;
function getUserFile(fileId) {
return __awaiter(this, void 0, void 0, function* () {
console.log(`[getUserFile] ${JSON.stringify({ fileId })}`);
const doc = yield siteCollection()
.collection('files').doc(fileId)
.get();
return doc.data();
});
}
exports.getUserFile = getUserFile;
function getFileContents(storagePath) {
return __awaiter(this, void 0, void 0, function* () {
console.log(`[getFileContents] ${JSON.stringify({ storagePath })}`);
const contentFile = yield admin.storage().bucket().file(storagePath);
const [contentExists] = yield contentFile.exists();
if (!contentExists) {
return null;
}
const [fileContent] = yield contentFile.download();
const bytesOfData = (fileContent.byteLength / 1024).toFixed(2);
console.log(`[getFileContents] File '${storagePath}' size: ${bytesOfData} kB`);
return fileContent;
});
}
exports.getFileContents = getFileContents;
function getSitemap() {
return __awaiter(this, void 0, void 0, function* () {
const siteInfo = (yield siteCollection().get()).data();
const documentsQuery = yield siteCollection()
.collection('documents')
.get();
const entries = [];
documentsQuery.forEach(doc => {
const document = doc.data();
const url = document.url;
const updated = document.updated.toDate();
const created = document.created.toDate();
const daysOfExistence = Math.round((updated - created) / (1000 * 60 * 60 * 24));
const changeFrequency = daysOfExistence / (document.revision + 1);
let sitemapChangeFreq;
if (changeFrequency < 7) {
sitemapChangeFreq = 'daily';
}
else if (changeFrequency < 30) {
sitemapChangeFreq = 'weekly';
}
else if (changeFrequency < 365) {
sitemapChangeFreq = 'monthly';
}
else {
sitemapChangeFreq = 'yearly';
}
entries.push(`
<url>
<loc>https://${siteInfo.primaryDomain}/${url}</loc>
<lastmod>${document.updated.toDate().toISOString().substr(0, 10)}</lastmod>
<changefreq>${sitemapChangeFreq}</changefreq>
</url>
`);
});
return `<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">${entries.join('')}</urlset>`;
});
}
exports.getSitemap = getSitemap;
const supportedContentTypes = {
'audio/aac': /\.(aac)$/i,
'application/x-abiword': /\.(abw)$/i,
'application/octet-stream': /\.(arc|bin)$/i,
'video/x-msvideo': /\.(avi)$/i,
'application/vnd.amazon.ebook': /\.(azw)$/i,
'application/x-bzip': /\.(bz)$/i,
'application/x-bzip2': /\.(bz2)$/i,
'application/x-csh': /\.(csh)$/i,
'text/csv; charset=utf-8': /\.(csv)$/i,
'application/msword': /\.(doc)$/i,
'application/vnd.openxmlformats-officedocument.wordprocessingml.document': /\.(docx)$/i,
'application/vnd.ms-fontobject': /\.(eot)$/i,
'application/epub+zip': /\.(epub)$/i,
'application/ecmascript; charset=utf-8': /\.(es)$/i,
'text/calendar; charset=utf-8': /\.(ics)$/i,
'application/java-archive': /\.(jar)$/i,
'audio/midi': /\.(mid|midi)$/i,
'video/mpeg': /\.(mpeg)$/i,
'application/vnd.apple.installer+xml': /\.(mpkg)$/i,
'application/vnd.oasis.opendocument.presentation': /\.(odp)$/i,
'application/vnd.oasis.opendocument.spreadsheet': /\.(ods)$/i,
'application/vnd.oasis.opendocument.text': /\.(odt)$/i,
'audio/ogg': /\.(oga)$/i,
'video/ogg': /\.(ogv)$/i,
'application/ogg': /\.(ogx)$/i,
'application/pdf': /\.(pdf)$/i,
'application/vnd.ms-powerpoint': /\.(ppt)$/i,
'application/vnd.openxmlformats-officedocument.presentationml.presentation': /\.(pptx)$/i,
'application/x-rar-compressed': /\.(rar)$/i,
'application/rtf': /\.(rtf)$/i,
'application/x-sh; charset=utf-8': /\.(sh)$/i,
'application/x-tar': /\.(tar)$/i,
'application/typescript; charset=utf-8': /\.(ts|d\.ts)$/i,
'application/vnd.visio': /\.(vsd)$/i,
'audio/wav': /\.(wav)$/i,
'audio/webm': /\.(weba)$/i,
'video/webm': /\.(webm)$/i,
'image/webp': /\.(webp)$/i,
'application/vnd.ms-excel': /\.(xls)$/i,
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': /\.(xlsx)$/i,
'application/xml; charset=utf-8': /\.(xml)$/i,
'application/vnd.mozilla.xul+xml': /\.(xul)$/i,
'application/zip': /\.(zip)$/i,
'application/x-7z-compressed': /\.(7z)$/i,
'font/otf': /\.(otf)$/i,
'font/ttf': /\.(ttf)$/i,
'font/woff': /\.(woff)$/i,
'font/woff2': /\.(woff2)$/i,
'image/jpeg': /\.(jpg|jpeg)$/i,
'image/gif': /\.(gif)$/i,
'image/png': /\.(png)$/i,
'image/tiff': /\.(tif|tiff)$/i,
'image/bmp': /\.(bmp)$/i,
'image/ico': /\.(ico)$/i,
'image/svg+xml': /\.(svg)$/i,
'text/plain; charset=utf-8': /\.(txt)$/i,
'text/css; charset=utf-8': /\.(css)$/i,
'text/javascript; charset=utf-8': /\.(js|js\.map)$/i,
'application/json; charset=utf-8': /\.(json)$/i,
'text/template': /\.(dust|hbs|ejs)$/i
};
function getContentTypeFromPath(filePath) {
console.log(`[getContentTypeFromPath] Resolving content type for: ${filePath}`);
for (const contentType in supportedContentTypes) {
if (supportedContentTypes[contentType].test(filePath)) {
console.log(`[getContentTypeFromPath] Content type ${contentType} for: ${filePath}`);
return contentType;
}
}
console.log(`[getContentTypeFromPath] No special content type found for: ${filePath}`);
return 'default';
}
exports.getContentTypeFromPath = getContentTypeFromPath;
//# sourceMappingURL=file.service.js.map