UNPKG

sheercms

Version:

Sheer Cliff CMS is a simple and powerful content management system (CMS) for Node JS.

127 lines (108 loc) 3.83 kB
app.service('PackageService', function($q, $http, $upload, DSCacheFactory) { var self = this; var pkgCache = DSCacheFactory('pkgCache', { maxAge: 1800000, // Items added to this cache expire after 30 minutes. cacheFlushInterval: 3600000, // This cache will clear itself every hour. deleteOnExpire: 'aggressive', // Items will be deleted from this cache right when they expire. storageMode: 'localStorage' // This cache will sync itself with `localStorage`. }); self.list = function(useCache) { var d = $q.defer(); var url = '/cms/packages/list'; var data = useCache ? pkgCache.get(url) : null; if (data) { d.resolve(data); } else { $http.get(url).success(function (data) { pkgCache.put(url, data); d.resolve(data); }).error(function (data, status) { d.reject(data); }); } return d.promise; }; self.create = function(name) { var d = $q.defer(); $http.post("/cms/packages/create", {name: name}).success(function(data) { pkgCache.removeAll(); d.resolve(data); }).error(function(data, status) { d.reject(data); }); return d.promise; }; self.update = function(pkg) { var d = $q.defer(); var json = JSON.stringify(pkg); $http.post("/cms/packages/update", {data:json}).success(function(data) { pkgCache.removeAll(); d.resolve(data); }).error(function(data, status) { d.reject(data); }); return d.promise; }; self.getPackage = function(id) { var d = $q.defer(); $http({ url:"/cms/packages/get", method: 'get', params: {packageId: id }}).success(function(data) { d.resolve(data); }).error(function(data, status) { d.reject(data); }); return d.promise; }; self.delete = function(id) { var d = $q.defer(); $http.post("/cms/packages/delete", {packageId: id}).success(function(data) { pkgCache.removeAll(); d.resolve(data); }).error(function(data, status) { d.reject(data); }); return d.promise; }; self.build = function(id) { var d = $q.defer(); $http.post("/cms/packages/build", {packageId: id}).success(function(data) { d.resolve(data); }).error(function(data, status) { d.reject(data); }); return d.promise; }; self.validate = function(id) { var d = $q.defer(); $http.post("/cms/packages/validate", {packageId: id}).success(function(data) { d.resolve(data); }).error(function(data, status) { d.reject(data); }); return d.promise; }; self.upload = function(file, onProgress, onError) { var d = $q.defer(); console.log("Uploading package file: " + file.name); $upload.upload({ url: '/cms/packages/upload', method: 'POST', data: { }, file: file }).progress(function(evt) { if (onProgress) onProgress(file.name, evt.loaded, evt.total); }).success(function(data, status, headers, config) { if (data && data.success == false && onError) { onError(file.name, data.message); } //clear the cache on the client-side DSCacheFactory.clearAll(); d.resolve(data); }).error(function(error) { if (onError) onError(error); d.resolve({success: false, message: error.message}); }); return d.promise; }; });