tanam
Version:
Pluggable CMS for Firebase
76 lines • 4.33 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 functions = require("firebase-functions");
const taskService = require("../services/task.service");
exports.onDeleteUserFile = functions.firestore.document('tanam/{siteId}/files/{fileId}').onDelete((snap, context) => __awaiter(this, void 0, void 0, function* () {
const siteId = context.params.siteId;
const fileId = context.params.fileId;
const file = snap.data();
const filePrefix = file.filePath.substr(0, file.filePath.lastIndexOf('.'));
console.log(`Delete all files starting with ${filePrefix}`);
const cacheUrls = [`/_/file/${fileId}`];
for (const key in file.variants) {
if (file.variants.hasOwnProperty(key)) {
cacheUrls.push(`/_/file/${fileId}?size=${key}`);
}
}
console.log(`Clearing cache: ${JSON.stringify(cacheUrls)}`);
return Promise.all([
...cacheUrls.map(url => taskService.deleteCache(siteId, url)),
admin.storage().bucket().deleteFiles({ prefix: filePrefix }),
]);
}));
exports.onCreateThemeAssetsFile = functions.firestore.document('tanam/{siteId}/themes/{themeId}/assets/{assetId}').onCreate((snap, context) => __awaiter(this, void 0, void 0, function* () {
const siteId = context.params.siteId;
const themeId = context.params.themeId;
console.log(`Creating file in ${JSON.stringify({ siteId, themeId })}`);
const siteInfo = (yield admin.firestore().collection('tanam').doc(siteId).get()).data();
console.log(`Active theme: ${siteInfo.theme}`);
if (siteInfo.theme !== themeId) {
console.log(`Creating file in a theme that is not active. No need to heat cache.`);
return null;
}
const file = snap.data();
return taskService.createCache(siteId, `/_/theme/${encodeURIComponent(file.title)}`);
}));
exports.onUpdateThemeAssetsFile = functions.firestore.document('tanam/{siteId}/themes/{themeId}/assets/{assetId}').onUpdate((change, context) => __awaiter(this, void 0, void 0, function* () {
const siteId = context.params.siteId;
const themeId = context.params.themeId;
console.log(`Updating file in ${JSON.stringify({ siteId, themeId })}`);
const siteInfo = (yield admin.firestore().collection('tanam').doc(siteId).get()).data();
console.log(`Active theme: ${siteInfo.theme}`);
if (siteInfo.theme !== themeId) {
console.log(`Updating file in a theme that is not active. No need to refresh cache.`);
return null;
}
const file = change.after.data();
return taskService.updateCache(siteId, `/_/theme/${encodeURIComponent(file.title)}`);
}));
exports.onDeleteThemeAssetsFile = functions.firestore.document('tanam/{siteId}/themes/{themeId}/assets/{assetId}').onDelete((snap, context) => __awaiter(this, void 0, void 0, function* () {
const siteId = context.params.siteId;
const themeId = context.params.themeId;
console.log(`Deleting file in ${JSON.stringify({ siteId, themeId })}`);
const siteInfo = (yield admin.firestore().collection('tanam').doc(siteId).get()).data();
console.log(`Active theme: ${siteInfo.theme}`);
if (siteInfo.theme !== themeId) {
console.log(`Updating file in a theme that is not active. No need to refresh cache.`);
return null;
}
const file = snap.data();
const filePrefix = file.filePath.substr(0, file.filePath.lastIndexOf('.'));
console.log(`Delete all asset files starting with ${filePrefix}`);
return Promise.all([
taskService.deleteCache(siteId, `/_/theme/${encodeURIComponent(file.title)}`),
admin.storage().bucket().file(file.filePath).delete(),
]);
}));
//# sourceMappingURL=file.js.map