示例#1
0
        public async Task <Template> Unlink(TemplateLink link) //CLONE
        {
            var entity = await _templateStore.Load(link.TemplateId);

            if (entity == null || !entity.Workspace.CanEdit(User))
            {
                throw new InvalidOperationException();
            }

            if (entity.Parent != null)
            {
                TemplateUtility tu = new TemplateUtility(entity.Parent.Detail);

                tu.Name = entity.Name;

                tu.LocalizeDiskPaths(entity.Workspace.GlobalId, entity.GlobalId);

                entity.Detail = tu.ToString();

                entity.Parent = null;

                await _templateStore.Update(entity);
            }

            return(Mapper.Map <Template>(entity, WithActor()));
        }
示例#2
0
        public async Task Export(int[] ids, string src, string dest)
        {
            if (!User.IsAdmin)
            {
                throw new InvalidOperationException();
            }

            var list = new List <Data.Workspace>();

            foreach (int id in ids)
            {
                var topo = await _workspaceStore.LoadWithParents(id);

                if (topo != null)
                {
                    list.Add(topo);
                }
            }

            // if (ids.Contains(0))
            // {
            //     list.Add(await _topoRepo.LoadAdminTopo());
            // }

            if (list.Count < 1)
            {
                return;
            }

            string docSrc = Path.Combine(src, "_docs");

            string docDest = Path.Combine(dest, "_docs");

            if (!Directory.Exists(dest))
            {
                Directory.CreateDirectory(dest);
            }

            if (!Directory.Exists(docDest))
            {
                Directory.CreateDirectory(docDest);
            }

            foreach (var topo in list)
            {
                string folder = Path.Combine(dest, topo.GlobalId);

                Directory.CreateDirectory(folder);

                File.WriteAllText(
                    Path.Combine(folder, "import.this"),
                    "please import this workspace"
                    );

                //export data
                topo.Workers.Clear();

                topo.Gamespaces.Clear();

                topo.Id = 0;

                topo.ShareCode = "";

                foreach (var template in topo.Templates)
                {
                    template.Id          = 0;
                    template.WorkspaceId = 0;
                    template.Workspace   = null;
                }

                File.WriteAllText(
                    Path.Combine(folder, "topo.json"),
                    JsonSerializer.Serialize(topo, jsonSerializerSettings)
                    );

                //export doc
                try
                {
                    CopyFile(
                        Path.Combine(docSrc, topo.GlobalId) + ".md",
                        Path.Combine(docDest, topo.GlobalId) + ".md"
                        );

                    CopyFolder(
                        Path.Combine(docSrc, topo.GlobalId),
                        Path.Combine(docDest, topo.GlobalId)
                        );
                } catch {}

                //export disk-list
                var disks = new List <string>();

                foreach (var template in topo.Templates)
                {
                    var tu = new TemplateUtility(template.Detail ?? template.Parent.Detail);
                    var t  = tu.AsTemplate();

                    foreach (var disk in t.Disks)
                    {
                        disks.Add(disk.Path);
                    }

                    if (t.Iso.HasValue())
                    {
                        disks.Add(t.Iso);
                    }
                }

                if (disks.Count > 0)
                {
                    File.WriteAllLines(
                        Path.Combine(folder, "topo.disks"),
                        disks.Distinct()
                        );
                }
            }
        }