Пример #1
0
        public void DeleteFromDb(string DiskFullPath, SiteDb sitedb)
        {
            var NonRoutable = DiskPathService.GetNonRoutableObject(DiskFullPath);

            if (NonRoutable != null)
            {
                var    repo   = sitedb.GetRepository(NonRoutable.StoreName);
                string name   = string.IsNullOrWhiteSpace(NonRoutable.Name) ? NonRoutable.Id.ToString() : NonRoutable.Name;
                var    result = repo.GetByNameOrId(name) as ISiteObject;
                if (result != null)
                {
                    repo.Delete(result.Id);
                }
            }
            else
            {
                var RelativeUrl = DiskPathService.GetRelativeUrl(sitedb.WebSite, DiskFullPath);
                var route       = sitedb.Routes.GetByUrl(RelativeUrl);
                if (route != null)
                {
                    var repo = sitedb.GetRepository(route.DestinationConstType);
                    if (repo != null)
                    {
                        var result = repo.Get(route.objectId) as ISiteObject;
                        if (result != null)
                        {
                            repo.Delete(result.Id);
                        }
                    }
                }
            }
        }
Пример #2
0
        public void SyncToDb(string FullPath, SiteDb SiteDb, byte[] diskbytes = null)
        {
            if (diskbytes == null)
            {
                diskbytes = this.ReadAllBytes(FullPath);
            }
            if (diskbytes == null)
            {
                return;
            }

            string OldRelativeUrl = null;
            string RelativeUrl    = null;

            IRepository repo   = null;
            ISiteObject result = null;

            Routing.Route route        = null;
            string        NameFromFile = null;
            string        extension    = UrlHelper.FileExtension(FullPath);

            if (!string.IsNullOrEmpty(extension) && !extension.StartsWith("."))
            {
                extension = "." + extension;
            }
            if (!string.IsNullOrEmpty(extension))
            {
                extension = extension.ToLower();
            }

            var NonRoutable = DiskPathService.GetNonRoutableObject(FullPath);

            if (NonRoutable != null)
            {
                repo         = SiteDb.GetRepository(NonRoutable.StoreName);
                NameFromFile = NonRoutable.Name;
                string name = string.IsNullOrWhiteSpace(NonRoutable.Name) ? NonRoutable.Id.ToString() : NonRoutable.Name;
                if (!string.IsNullOrEmpty(NonRoutable.Extension))
                {
                    extension = NonRoutable.Extension.ToLower();
                    if (!extension.StartsWith("."))
                    {
                        extension = "." + extension;
                    }
                }

                result = repo.GetByNameOrId(name) as ISiteObject;

                if (result == null)
                {
                    if (name.ToLower().EndsWith(extension))
                    {
                        name   = name.Substring(0, name.Length - extension.Length);
                        result = repo.GetByNameOrId(name);
                    }
                    else
                    {
                        name   = name + extension;
                        result = repo.GetByNameOrId(name);
                    }
                }
            }
            else
            {
                OldRelativeUrl = DiskPathService.GetRelativeUrl(SiteDb.WebSite, FullPath);
                RelativeUrl    = Kooboo.Sites.Helper.RouteHelper.ToValidRoute(OldRelativeUrl);

                route = SiteDb.Routes.GetByUrl(RelativeUrl);
                if (route != null)
                {
                    repo   = SiteDb.GetRepository(route.DestinationConstType);
                    result = repo.Get(route.objectId) as ISiteObject;
                }
                else
                {
                    var ModelType = Service.ConstTypeService.GetModelTypeByUrl(RelativeUrl);
                    if (ModelType == null)
                    {
                        return;
                    }
                    repo = SiteDb.GetRepository(ModelType);
                }
                NameFromFile = UrlHelper.FileName(RelativeUrl);
            }

            if (result == null)
            {
                result = Activator.CreateInstance(repo.ModelType) as ISiteObject;
            }

            if (!DiskObjectConverter.FromBytes(ref result, diskbytes, SiteDb))
            {
                return;
            }

            if (result is IExtensionable)
            {
                var extensionfile = result as IExtensionable;
                extensionfile.Extension = extension;
            }

            if (string.IsNullOrEmpty(result.Name))
            {
                result.Name = Lib.Helper.StringHelper.ToValidFileName(NameFromFile);
            }

            #region "Routing"

            if (!string.IsNullOrEmpty(RelativeUrl))
            {
                SiteDb.Routes.AddOrUpdate(RelativeUrl, result as SiteObject);
            }
            else
            {
                // # Rule1, only the API is different...
                if (result is Kooboo.Sites.Models.Code)
                {
                    var code = result as Code;
                    if (code.CodeType == CodeType.Api)
                    {
                        bool shouldUpdateCodeRouteText = false;

                        var diskroute = DiskObjectConverter.GetRouteFromCodeBytes(diskbytes);
                        if (string.IsNullOrWhiteSpace(diskroute))
                        {
                            // # Rule2, Api must have a route defined, otherwise it is a new api.
                            var newroute = DiskObjectConverter.GetNewRoute(SiteDb, code.Name);
                            SiteDb.Routes.AddOrUpdate(newroute, code);
                            shouldUpdateCodeRouteText = true;
                        }
                        else
                        {
                            // # Rule 3, Check if this is its own route, or someelse routes.
                            // Own rule, do nothing.
                            var coderoute = SiteDb.Routes.Get(diskroute);

                            if (coderoute != null && coderoute.objectId != default(Guid) && coderoute.objectId != code.Id)
                            {
                                //Someone else route already. make a new one
                                diskroute = DiskObjectConverter.GetNewRoute(SiteDb, diskroute);
                                coderoute = null;
                                shouldUpdateCodeRouteText = true;
                            }

                            if (coderoute == null)
                            {
                                //#Rule 4, If route does not exists yet. Add and delte old one if any.
                                var oldroute = SiteDb.Routes.GetByObjectId(code.Id);
                                SiteDb.Routes.AddOrUpdate(diskroute, code);
                                if (oldroute != null)
                                {
                                    SiteDb.Routes.Delete(oldroute.Id);
                                }
                            }
                            else
                            {
                                // donothing.
                            }
                        }

                        if (shouldUpdateCodeRouteText)
                        {
                            this.SyncToDisk(SiteDb, code, ChangeType.Update, SiteDb.Code.StoreName);
                        }
                    }
                }
            }

            #endregion

            if (!isSameName(result.Name, NameFromFile, extension) || OldRelativeUrl != RelativeUrl)
            {
                if (File.Exists(FullPath))
                {
                    File.Delete(FullPath);
                }
                repo.AddOrUpdate(result);
            }

            else
            {
                repo.AddOrUpdate(result);
            }
        }