コード例 #1
0
        protected bool DeleteFile(string path, string name, string extention)
        {
            path = AuthorizeManager.AuthorizeActionOnPath(path.Replace("//", "/"), ActionKey.DeleteFromDisk);


            return(_fileSystemManager.DeleteFile(path + name + extention));
        }
コード例 #2
0
        private void CreateThumbnail(List <DiskInfo> files)
        {
            var rootPath = HostingEnvironment.ApplicationHost.GetPhysicalPath();

            var tempPath = _fileSystemManager.RelativeToAbsolutePath(AuthorizeManager.AuthorizeActionOnPath(Config.ThumbnailPath, ActionKey.WriteToDisk));

            if (tempPath == null)
            {
                throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.PathNotFound, Config.ThumbnailPath));
            }

            var thumbnailPath = tempPath.ToLower();

            Parallel.ForEach(files, file =>
            {
                var thumbPath = thumbnailPath +
                                file.FullName.Substring(rootPath.Length);
                if (_fileSystemManager.FileExist(thumbPath) ||
                    file.FullName.ToLower().IndexOf(thumbnailPath, StringComparison.Ordinal) != -1)
                {
                    return;
                }
                var thumbImg = _imageManager.CreateThumbnail(Image.FromFile(file.FullName, true),
                                                             200, 200);
                thumbImg.Save(thumbPath);
            });
        }
コード例 #3
0
        public async Task Save(JObject data)
        {
            dynamic dataDto = data;

            string path    = dataDto.Path;
            string name    = dataDto.Name;
            string content = dataDto.Content;
            await _fileSystemManager.WriteAsync(AuthorizeManager.AuthorizeActionOnPath(path + "/" + name, ActionKey.WriteToDisk), content);
        }
コード例 #4
0
        public List <ZipInfo> OpenZip(string zipFullName, string orderBy, int skip, int take, out int count)
        {
            //if (zipFullName[0] != '~')
            //    zipFullName = zipFullName[0] == '/' ? "~" +
            //        zipFullName : "~/" + zipFullName;


            return(_zipManager.OpenZip(AuthorizeManager.AuthorizeActionOnPath(zipFullName.Replace(Config.UrlDelimeter, Helper.RootUrl), ActionKey.ReadFromDisk),
                                       orderBy, skip, take, out count));
        }
コード例 #5
0
 public async Task <string> GetFileContenAsync(string path)
 {
     path = path.Replace(Config.UrlDelimeter, Helper.RootUrl);
     path = AuthorizeManager.AuthorizeActionOnPath(path, ActionKey.ReadFromDisk);
     if (await _fileSystemManager.FileExistAsync(path))
     {
         return(await _fileSystemManager.ReadAsync(path));
     }
     throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.FileNotFound));
 }
コード例 #6
0
        private async Task <string> GetResorces(LanguageAndCulture languageAndCulture)
        {
            var path = AuthorizeManager.AuthorizeActionOnPath(Config.ResourcesSourceCodePath, ActionKey.ReadFromDisk) + languageAndCulture.Language + ".js";


            if (await _fileSystemManager.FileExistAsync(path))
            {
                return(await _fileSystemManager.ReadAsync(path));
            }

            return("");
        }
コード例 #7
0
        public List <DiskInfo> GetPathDirectoriesAndFiles(string path, string orderBy, int skip, int take, out int count, bool byFile = true, string searchPatern = "*", bool allDirectories = false, bool createThumbnail = false)
        {
            path = AuthorizeManager.AuthorizeActionOnPath(path.Replace(Config.UrlDelimeter, Helper.RootUrl), ActionKey.ReadFromDisk);

            var diskInfos    = new List <DiskInfo>();
            var searchOption = allDirectories ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
            var directories  = _fileSystemManager.GetDirectories(path, searchPatern, searchOption);
            var files        = _fileSystemManager.GetFiles(path, searchPatern, searchOption);
            var id           = 0;

            foreach (var directory in directories)
            {
                diskInfos.Add(new DiskInfo()
                {
                    Id                   = ++id,
                    Name                 = directory.Name,
                    IsFolder             = true,
                    ModifieDateTime      = directory.LastWriteTimeUtc,
                    ModifieLocalDateTime = LanguageManager.ToLocalDateTime(directory.LastWriteTimeUtc)
                });
            }
            var rootPath = HostingEnvironment.ApplicationHost.GetPhysicalPath();

            if (byFile)
            {
                foreach (var file in files)
                {
                    diskInfos.Add(new DiskInfo()
                    {
                        Id                   = ++id,
                        Name                 = file.Name,
                        FileType             = _fileSystemManager.FileExtensionToFileType(file.Extension),
                        FullName             = file.FullName,
                        ModifieDateTime      = file.LastWriteTimeUtc,
                        Size                 = file.Length,
                        ModifieLocalDateTime = LanguageManager.ToLocalDateTime(file.LastWriteTimeUtc)
                    });
                }
            }
            count = diskInfos.Count();
            var fileAndFolders = diskInfos.AsQueryable().OrderBy(orderBy)
                                 .Skip(skip)
                                 .Take(take).ToList();

            if (createThumbnail)
            {
                var t = new Thread(tr => CreateThumbnail(fileAndFolders.Where(fl => fl.IsFolder == false && fl.FileType == 1).ToList()));
                t.Start();
            }

            return(fileAndFolders);
        }
コード例 #8
0
        public bool DownlodFromUrl(int baseUrlId, string url, string filePath, string fileName)
        {
            filePath = AuthorizeManager.AuthorizeActionOnPath(filePath
                                                              .Replace(Config.UrlDelimeter, Helper.RootUrl), ActionKey.WriteToDisk);
            if (AuthorizeManager.AuthorizeActionOnEntityId(baseUrlId, (int)EntityIdentity.Urls,
                                                           (int)ActionKey.DownloadFromAddress))
            {
                var baseUrl = _contentManagementContext.MasterDataKeyValues.AsNoTracking()
                              .SingleOrDefault(md => md.TypeId == (int)EntityIdentity.Urls && md.Id == baseUrlId);
                if (baseUrl == null)
                {
                    throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.CodeNotFound));
                }

                var downloadUrl = "";
                if (baseUrl.PathOrUrl[baseUrl.PathOrUrl.Length - 1] == '/')
                {
                    if (url[0] == '/')
                    {
                        downloadUrl = baseUrl.PathOrUrl + url.Substring(1);
                    }
                    else
                    {
                        downloadUrl = baseUrl.PathOrUrl + url;
                    }
                }
                else
                {
                    if (url[0] == '/')
                    {
                        downloadUrl = baseUrl.PathOrUrl + url;
                    }
                    else
                    {
                        downloadUrl = baseUrl.PathOrUrl + "/" + url;
                    }
                }

                if (filePath[filePath.Length - 1] == '/')
                {
                    filePath += fileName;
                }
                else
                {
                    filePath += "/" + fileName;
                }

                _fileSystemManager.DownlodFromUrl(downloadUrl,
                                                  filePath);
            }
            return(true);
        }
コード例 #9
0
        protected async Task WriteFileAsync(string path, string name, string extention,
                                            string content, bool creatDirectoryIfNotExist = false)
        {
            path = AuthorizeManager.AuthorizeActionOnPath(path.Replace("//", "/"), ActionKey.WriteToDisk);



            await
            _fileSystemManager.WriteAsync(
                (creatDirectoryIfNotExist
                    ? _fileSystemManager.CreatDirectoryIfNotExist(path)
                    : _fileSystemManager.RelativeToAbsolutePath(path)) + name + extention, content);
        }
コード例 #10
0
        public bool Rename(RenameOprationInfo renameInfo)
        {
            var rootPath = HostingEnvironment.ApplicationHost.GetPhysicalPath();
            var tempPath = _fileSystemManager.RelativeToAbsolutePath(Config.ThumbnailPath);

            if (tempPath == null)
            {
                throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.PathNotFound, Config.ThumbnailPath));
            }

            var thumbnailRootPath = tempPath.ToLower();

            var oldPath = _fileSystemManager.RelativeToAbsolutePath(AuthorizeManager.AuthorizeActionOnPath(renameInfo.OldPath, ActionKey.ReadFromDisk));
            var newPath = _fileSystemManager.RelativeToAbsolutePath(AuthorizeManager.AuthorizeActionOnPath(renameInfo.NewPath, ActionKey.WriteToDisk));

            if (oldPath == null)
            {
                throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.PathNotFound, renameInfo.OldPath));
            }

            if (newPath == null)
            {
                throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.PathNotFound, renameInfo.NewPath));
            }


            var thumbnailOldPath = oldPath.Substring(rootPath.Length);
            var thumbnailNewPath = newPath.Substring(rootPath.Length);



            if (!renameInfo.IsDirectory)
            {
                _fileSystemManager.RenameFile(oldPath, newPath);
                if (_fileSystemManager.FileExist(thumbnailRootPath + thumbnailOldPath))
                {
                    _fileSystemManager.RenameFile(thumbnailRootPath + thumbnailOldPath, thumbnailRootPath + thumbnailNewPath);
                }
            }
            else
            {
                _fileSystemManager.RenameDirectory(oldPath, newPath);
                if (_fileSystemManager.DirectoryExists(thumbnailRootPath + thumbnailOldPath))
                {
                    _fileSystemManager.RenameDirectory(thumbnailRootPath + thumbnailOldPath, thumbnailRootPath + thumbnailNewPath);
                }
            }
            return(true);
        }
コード例 #11
0
        public bool UnZip(UnZipOprationInfo unZipOprationInfo)
        {
            //if (unZipOprationInfo.DestinationPath[0] != '~')
            //    unZipOprationInfo.DestinationPath = unZipOprationInfo.DestinationPath[0] == '/' ? "~" +
            //        unZipOprationInfo.DestinationPath : "~/" + unZipOprationInfo.DestinationPath;
            //if (unZipOprationInfo.SourcePath[0] != '~')
            //    unZipOprationInfo.SourcePath = unZipOprationInfo.SourcePath[0] == '/' ? "~" +
            //        unZipOprationInfo.SourcePath : "~/" + unZipOprationInfo.SourcePath;

            unZipOprationInfo.DestinationPath = _fileSystemManager.RelativeToAbsolutePath(AuthorizeManager.AuthorizeActionOnPath(unZipOprationInfo.DestinationPath, ActionKey.WriteToDisk));

            unZipOprationInfo.SourcePath = _fileSystemManager.RelativeToAbsolutePath(AuthorizeManager.AuthorizeActionOnPath(unZipOprationInfo.SourcePath, ActionKey.ReadFromDisk));

            return(_zipManager.UnZip(unZipOprationInfo));
        }
コード例 #12
0
        private string Transform(MasterDataKeyValue bundle, string localHost, string source, string dist)
        {
            FileSystemManager.CreatDirectoryIfNotExist(AuthorizeManager.AuthorizeActionOnPath
                                                           (dist.Substring(0, dist.LastIndexOf("/", StringComparison.Ordinal)),
                                                           ActionKey.WriteToDisk));
            var bundlePath = source.ToLower().Replace("~/", "");

            var bundleOption = new List <BundleOption>
            {
                new BundleOption()
                {
                    Url     = "~/BrowsersCodeOutPut/" + bundle.Guid + "/" + bundle.Version + "/" + bundlePath,
                    Sources = new List <string>()
                    {
                        source
                    }
                }
            };


            foreach (var option in bundleOption)
            {
                foreach (var sourceUrl in option.Sources)
                {
                    AuthorizeManager.AuthorizeActionOnPath(sourceUrl, ActionKey.ReadFromDisk);
                }
            }



            _bundleManager.AddBundle(bundleOption);


            var bundleNmae = "~/BrowsersCodeOutPut/" + bundle.Guid + "/" + bundle.Version + "/" + bundlePath
                             .Replace(".", "-");
            var    url = bundleNmae.Replace("~", localHost);
            string contents;

            using (var wc = new System.Net.WebClient())
            {
                wc.Encoding = Encoding.UTF8;
                contents    = wc.DownloadString(url);
            }

            _bundleManager.RemoveBundle(bundleNmae);
            return(contents);
        }
コード例 #13
0
        public bool Delete(DeleteOprationInfo deleteInfo)
        {
            var rootPath = HostingEnvironment.ApplicationHost.GetPhysicalPath();
            var tempPath = _fileSystemManager.RelativeToAbsolutePath(Config.ThumbnailPath);

            if (tempPath == null)
            {
                throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.PathNotFound, Config.ThumbnailPath));
            }

            var thumbnailRootPath = tempPath.ToLower();

            var path = _fileSystemManager.RelativeToAbsolutePath(AuthorizeManager.AuthorizeActionOnPath(deleteInfo.Path, ActionKey.DeleteFromDisk));

            if (path == null)
            {
                throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.PathNotFound, deleteInfo.Path));
            }



            Parallel.ForEach(deleteInfo.Files, file =>
            {
                var realPath = path + "/" + file;
                _fileSystemManager.DeleteFile(realPath);

                var thumbnailPath = realPath.Substring(rootPath.Length);

                if (_fileSystemManager.FileExist(thumbnailRootPath + thumbnailPath))
                {
                    _fileSystemManager.DeleteFile(thumbnailRootPath + thumbnailPath);
                }
            });
            Parallel.ForEach(deleteInfo.Folders, folder =>
            {
                var realPath = path + "/" + folder;
                _fileSystemManager.DeleteDirectory(realPath);

                var thumbnailPath = realPath.Substring(rootPath.Length);

                if (_fileSystemManager.DirectoryExists(thumbnailRootPath + thumbnailPath))
                {
                    _fileSystemManager.DeleteDirectory(thumbnailRootPath + thumbnailPath);
                }
            });
            return(true);
        }
コード例 #14
0
        public async Task <string> GetCodeContentAsync(int codeId, string path)
        {
            path = path.Replace(Config.UrlDelimeter, Helper.RootUrl);
            var code = ContentManagementContext.MasterDataKeyValues.FirstOrDefault(sr => sr.Id == codeId);

            if (code == null || !path.ToLower().StartsWith(code.PathOrUrl.ToLower()))
            {
                throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.CodeNotFound));
            }
            AuthorizeManager.SetAndCheckModifyAndAccessRole(code, null, false);
            if (await FileSystemManager.FileExistAsync(AuthorizeManager
                                                       .AuthorizeActionOnPath(path, ActionKey.ReadFromDisk)))
            {
                return(await FileSystemManager.ReadAsync(path));
            }
            throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.CodeNotFound));
        }
コード例 #15
0
        public bool Copy(DiskOprationInfo copyInfo)
        {
            //if (copyInfo.DestinationPath[0] != '~')
            //    copyInfo.DestinationPath = copyInfo.DestinationPath[0] == '/' ? "~" + copyInfo.DestinationPath : "~/" + copyInfo.DestinationPath;
            //if (copyInfo.SourcePath[0] != '~')
            //    copyInfo.SourcePath = copyInfo.SourcePath[0] == '/' ? "~" + copyInfo.SourcePath : "~/" + copyInfo.SourcePath;

            var destinationPath = AuthorizeManager.AuthorizeActionOnPath(copyInfo.DestinationPath, ActionKey.WriteToDisk);
            var sourcePath      = AuthorizeManager.AuthorizeActionOnPath(copyInfo.SourcePath, ActionKey.ReadFromDisk);

            Parallel.ForEach(copyInfo.Files, file =>
            {
                _fileSystemManager.CopyFile(sourcePath + "/" + file, destinationPath + "/" + file, copyInfo.OverWrite);
            });
            Parallel.ForEach(copyInfo.Folders, folder =>
            {
                _fileSystemManager.CopyDirectory(sourcePath + "/" + folder, destinationPath + "/" + folder, copyInfo.OverWrite);
            });
            return(true);
        }
コード例 #16
0
        public bool Zip(ZipOprationInfo zipOprationInfo)
        {
            //if (zipOprationInfo.DestinationPath[0] != '~')
            //    zipOprationInfo.DestinationPath = zipOprationInfo.DestinationPath[0] == '/' ? "~" +
            //        zipOprationInfo.DestinationPath : "~/" + zipOprationInfo.DestinationPath;
            //if (zipOprationInfo.SourcePath[0] != '~')
            //    zipOprationInfo.SourcePath = zipOprationInfo.SourcePath[0] == '/' ? "~" +
            //        zipOprationInfo.SourcePath : "~/" + zipOprationInfo.SourcePath;

            var temp = zipOprationInfo.DestinationPath;

            zipOprationInfo.DestinationPath = _fileSystemManager.RelativeToAbsolutePath(AuthorizeManager.AuthorizeActionOnPath(zipOprationInfo.DestinationPath, ActionKey.WriteToDisk));
            if (zipOprationInfo.DestinationPath == null)
            {
                throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.PathNotFound, temp));
            }


            zipOprationInfo.SourcePath = _fileSystemManager.RelativeToAbsolutePath(AuthorizeManager.AuthorizeActionOnPath(zipOprationInfo.SourcePath, ActionKey.ReadFromDisk));

            if (!zipOprationInfo.OverWrite)
            {
                if (_fileSystemManager.FileExist(zipOprationInfo.DestinationPath.ToLower().IndexOf(".zip", StringComparison.Ordinal) > -1
                    ? zipOprationInfo.DestinationPath : zipOprationInfo.DestinationPath + ".zip"))
                {
                    throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.RepeatedPath, zipOprationInfo.DestinationPath));
                }
            }
            var path = zipOprationInfo.DestinationPath.Remove(zipOprationInfo.DestinationPath.ToLower()
                                                              .LastIndexOf("\\", StringComparison.Ordinal));

            if (!_fileSystemManager.DirectoryExists(path))
            {
                throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.PathNotFound, path));
            }

            return(_zipManager.Zip(zipOprationInfo));
        }
コード例 #17
0
        public async Task <bool> SaveFile(JObject data)
        {
            //var user = "******";
            //var version = "_asV_";
            dynamic codeDto     = data;
            int     id          = codeDto.Id;
            bool    checkIn     = codeDto.CheckIn;
            string  path        = codeDto.Path;
            string  codeContent = codeDto.Code;
            string  comment     = codeDto.Comment;

            path = path.Replace(Config.UrlDelimeter, Helper.RootUrl);
            var code = ContentManagementContext.MasterDataKeyValues.FirstOrDefault(sr => sr.Id == id);

            if (code == null)
            {
                throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.CodeNotFound));
            }

            if (code.EditMode)
            {
                SourceControl.CheckCodeCheckOute(code);
            }

            AuthorizeManager.SetAndCheckModifyAndAccessRole(code, null, false);

            if (await FileSystemManager.FileExistAsync(AuthorizeManager
                                                       .AuthorizeActionOnPath(path, ActionKey.WriteToDisk)))
            {
                code.Version++;
                await ContentManagementContext.SaveChangesAsync();


                await SourceControl.AddChange(path.Remove(path.LastIndexOf("/", StringComparison.Ordinal) + 1),
                                              path.Substring(path.LastIndexOf("/", StringComparison.Ordinal) + 1),
                                              codeContent,
                                              code.Version,
                                              comment);


                //var userIndex = path.IndexOf(user, StringComparison.Ordinal);
                //var endSourceControlStringIndex = path.LastIndexOf(".", StringComparison.Ordinal);
                //if (userIndex > 0 &&
                //    path.IndexOf(version, StringComparison.Ordinal) > 0)
                //{
                //    path = path.Remove(userIndex, endSourceControlStringIndex - userIndex);


                //}
                //else
                //{
                if (checkIn)
                {
                    await FileSystemManager.WriteAsync(path, codeContent);
                }
                //}

                //return await FileSystemManager.WriteAsync(path.Insert(path.LastIndexOf(".", StringComparison.Ordinal),
                //     user+ CurrentUserManager.UserName.Replace("@", "[at]")+version+code.Version), codeContent);
                return(true);
            }

            throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.CodeNotFound));
        }
コード例 #18
0
 protected bool DeleteFile(string path)
 {
     return(FileSystemManager.DeleteFile(AuthorizeManager.AuthorizeActionOnPath(path, ActionKey.DeleteFromDisk)));
 }
コード例 #19
0
 public bool DeleteDirectory(string path)
 {
     return(_fileSystemManager.DeleteDirectory(AuthorizeManager.AuthorizeActionOnPath(path, ActionKey.DeleteFromDisk)));
 }
コード例 #20
0
 public bool CreateDirectory(string path)
 {
     _fileSystemManager.CreatDirectoryIfNotExist(AuthorizeManager.AuthorizeActionOnPath(path, ActionKey.WriteToDisk));
     return(true);
 }
コード例 #21
0
        public bool Move(DiskOprationInfo moveInfo)
        {
            //if (moveInfo.DestinationPath[0] != '~')
            //    moveInfo.DestinationPath = moveInfo.DestinationPath[0] == '/' ? "~" + moveInfo.DestinationPath : "~/" + moveInfo.DestinationPath;
            //if (moveInfo.SourcePath[0] != '~')
            //    moveInfo.SourcePath = moveInfo.SourcePath[0] == '/' ? "~" + moveInfo.SourcePath : "~/" + moveInfo.SourcePath;
            var rootPath = HostingEnvironment.ApplicationHost.GetPhysicalPath();
            var tempPath = _fileSystemManager.RelativeToAbsolutePath(Config.ThumbnailPath);

            if (tempPath == null)
            {
                throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.PathNotFound, Config.ThumbnailPath));
            }

            var thumbnailRootPath = tempPath.ToLower();

            var destinationPath = AuthorizeManager.AuthorizeActionOnPath(moveInfo.DestinationPath, ActionKey.WriteToDisk);
            var sourcePath      = AuthorizeManager.AuthorizeActionOnPath(moveInfo.SourcePath, ActionKey.ReadFromDisk);


            Parallel.ForEach(moveInfo.Files, file =>
            {
                var source = _fileSystemManager.RelativeToAbsolutePath(sourcePath + "/" + file);
                var dist   = _fileSystemManager.RelativeToAbsolutePath(destinationPath + "/" + file);
                if (source == null)
                {
                    throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.PathNotFound, "source"));
                }

                if (dist == null)
                {
                    throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.PathNotFound, "dist"));
                }

                _fileSystemManager.MoveFile(source, dist);

                var thumbnailSourcePath = source.Substring(rootPath.Length);
                var thumbnailDistPath   = dist.Substring(rootPath.Length);
                if (_fileSystemManager.FileExist(thumbnailRootPath + thumbnailSourcePath))
                {
                    _fileSystemManager.MoveFile(thumbnailRootPath + thumbnailSourcePath, thumbnailRootPath + thumbnailDistPath);
                }
            });
            Parallel.ForEach(moveInfo.Folders, folder =>
            {
                var source = _fileSystemManager.RelativeToAbsolutePath(sourcePath + "/" + folder);
                var dist   = _fileSystemManager.RelativeToAbsolutePath(destinationPath + "/" + folder);
                if (source == null)
                {
                    throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.PathNotFound, "source"));
                }

                if (dist == null)
                {
                    throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.PathNotFound, "dist"));
                }

                _fileSystemManager.MoveDirectory(source, dist);

                var thumbnailSourcePath = source.Substring(rootPath.Length);
                var thumbnailDistPath   = dist.Substring(rootPath.Length);
                if (_fileSystemManager.DirectoryExists(thumbnailRootPath + thumbnailSourcePath))
                {
                    _fileSystemManager.MoveDirectory(thumbnailRootPath + thumbnailSourcePath, thumbnailRootPath + thumbnailDistPath);
                }
            });
            return(true);
        }
コード例 #22
0
        public async Task <bool> Compile(JObject data, string localHost)
        {
            dynamic bundleDto = data;
            int     id        = bundleDto.Id;
            bool    isPublish = bundleDto.IsPublish;
            //string buildJs = "";

            var bundleBySources = await ContentManagementContext.MasterDataKeyValues.Where(cd => cd.Id == id ||
                                                                                           (cd.ParentId == id && cd.TypeId == (int)EntityIdentity.BundleSource)).ToListAsync();

            var bundle = bundleBySources.FirstOrDefault(bn => bn.Id == id);

            var sources = bundleBySources.Where(sr => sr.ParentId == id).ToList();

            if (bundle == null)
            {
                throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.BundleNotFound));
            }
            if (sources.Count == 0)
            {
                throw new KhodkarInvalidException(LanguageManager.ToAsErrorMessage(ExceptionKey.BundleHasNoSource));
            }
            CheckAccess(bundle);

            var code = await ContentManagementContext.MasterDataKeyValues.FirstOrDefaultAsync(cd => cd.Id == bundle.ParentId);

            if (code.EditMode)
            {
                SourceControl.CheckCodeCheckOute(code);
            }

            bundle.Version++;
            await ContentManagementContext.SaveChangesAsync();

            if (bundle.Value == 1)
            {
                foreach (var source in sources)
                {
                    var debugpath = "";
                    if (source.PathOrUrl.IndexOf(".less", StringComparison.OrdinalIgnoreCase) > -1 ||
                        bundle.PathOrUrl.IndexOf(".sass", StringComparison.OrdinalIgnoreCase) > -1 ||
                        bundle.PathOrUrl.IndexOf(".scss", StringComparison.OrdinalIgnoreCase) > -1)
                    {
                        debugpath =
                            source.PathOrUrl.ToLower().Replace(code.PathOrUrl.ToLower(), Config.StyleDebugPath).Replace("//", "/");

                        debugpath = debugpath.Replace(".less", ".css").Replace(".sass", ".css").Replace(".scss", ".css");

                        FileSystemManager.CreatDirectoryIfNotExist(
                            AuthorizeManager.AuthorizeActionOnPath(
                                debugpath.Substring(0, debugpath.LastIndexOf("/", StringComparison.Ordinal)),
                                ActionKey.WriteToDisk));

                        await WriteFileAsync(debugpath,
                                             "", "", Transform(bundle, localHost, source.PathOrUrl, debugpath));


                        if (isPublish)
                        {
                            var minContent = "";

                            var distpath = source.PathOrUrl.ToLower().Replace(code.PathOrUrl.ToLower(), Config.StyleDistPath).Replace("//", "/");
                            distpath   = distpath.Replace(".less", ".css").Replace(".sass", ".css").Replace(".scss", ".css");
                            minContent = _compressManager.CompressCss(Transform(bundle, localHost, source.PathOrUrl, distpath));


                            FileSystemManager.CreatDirectoryIfNotExist(
                                AuthorizeManager.AuthorizeActionOnPath(
                                    distpath.Substring(0, distpath.LastIndexOf("/", StringComparison.Ordinal)),
                                    ActionKey.WriteToDisk));

                            await WriteFileAsync(distpath,
                                                 "", "", minContent);
                        }
                    }
                    else if (source.PathOrUrl.IndexOf(".js", StringComparison.OrdinalIgnoreCase) == -1 &&
                             source.PathOrUrl.IndexOf(".css", StringComparison.OrdinalIgnoreCase) == -1)
                    {
                        debugpath =
                            source.PathOrUrl.ToLower().Replace(code.PathOrUrl.ToLower(), Config.StyleDebugPath).Replace("//", "/");

                        debugpath = debugpath.Remove(debugpath.LastIndexOf(".", StringComparison.Ordinal)) + ".js";

                        FileSystemManager.CreatDirectoryIfNotExist(
                            AuthorizeManager.AuthorizeActionOnPath(
                                debugpath.Substring(0, debugpath.LastIndexOf("/", StringComparison.Ordinal)),
                                ActionKey.WriteToDisk));

                        await WriteFileAsync(debugpath,
                                             "", "", Transform(bundle, localHost, source.PathOrUrl, debugpath));



                        if (isPublish)
                        {
                            var minContent = "";

                            var distpath = source.PathOrUrl.ToLower().Replace(code.PathOrUrl.ToLower(), Config.ScriptDistPath).Replace("//", "/");
                            distpath   = debugpath.Remove(distpath.LastIndexOf(".", StringComparison.Ordinal)) + ".js";
                            minContent = _compressManager.CompressJavaScript(
                                await FileSystemManager.ReadAsync(AuthorizeManager.AuthorizeActionOnPath(source.PathOrUrl, ActionKey.ReadFromDisk)), source.PathOrUrl);



                            FileSystemManager.CreatDirectoryIfNotExist(
                                AuthorizeManager.AuthorizeActionOnPath(
                                    distpath.Substring(0, distpath.LastIndexOf("/", StringComparison.Ordinal)),
                                    ActionKey.WriteToDisk));

                            await WriteFileAsync(distpath,
                                                 "", "", minContent);
                        }
                    }
                    else
                    {
                        debugpath = source.PathOrUrl.IndexOf(".css", StringComparison.OrdinalIgnoreCase) > -1 ?
                                    source.PathOrUrl.ToLower().Replace(code.PathOrUrl.ToLower(), Config.StyleDebugPath).Replace("//", "/") :
                                    source.PathOrUrl.ToLower().Replace(code.PathOrUrl.ToLower(), Config.ScriptDebugPath).Replace("//", "/");

                        FileSystemManager.CreatDirectoryIfNotExist(AuthorizeManager.AuthorizeActionOnPath(
                                                                       debugpath.Substring(0, debugpath.LastIndexOf("/", StringComparison.Ordinal)),
                                                                       ActionKey.WriteToDisk));
                        FileSystemManager.CopyFile(AuthorizeManager.AuthorizeActionOnPath(
                                                       source.PathOrUrl, ActionKey.ReadFromDisk),
                                                   AuthorizeManager.AuthorizeActionOnPath(debugpath, ActionKey.WriteToDisk));



                        if (isPublish)
                        {
                            var distpath   = "";
                            var minContent = "";
                            if (source.PathOrUrl.IndexOf(".css", StringComparison.OrdinalIgnoreCase) > -1)
                            {
                                distpath   = source.PathOrUrl.ToLower().Replace(code.PathOrUrl.ToLower(), Config.StyleDistPath).Replace("//", "/");
                                minContent = _compressManager.CompressCss(await
                                                                          FileSystemManager.ReadAsync(AuthorizeManager.AuthorizeActionOnPath(source.PathOrUrl, ActionKey.ReadFromDisk)));
                            }
                            else
                            {
                                distpath = source.PathOrUrl.ToLower().Replace(code.PathOrUrl.ToLower(), Config.ScriptDistPath).Replace("//", "/");

                                minContent = _compressManager.CompressJavaScript(
                                    await FileSystemManager.ReadAsync(AuthorizeManager.AuthorizeActionOnPath(source.PathOrUrl, ActionKey.ReadFromDisk)), source.PathOrUrl);
                            }

                            FileSystemManager.CreatDirectoryIfNotExist(
                                AuthorizeManager.AuthorizeActionOnPath(
                                    distpath.Substring(0, distpath.LastIndexOf("/", StringComparison.Ordinal)),
                                    ActionKey.WriteToDisk));

                            await WriteFileAsync(distpath,
                                                 "", "", minContent);
                        }
                    }
                }

                return(true);
            }

            var bundlePath = bundle.PathOrUrl.ToLower().Replace("~/", "");

            var bundleOption = new List <BundleOption>
            {
                new BundleOption()
                {
                    Url     = "~/BrowsersCodeOutPut/" + bundle.Guid + "/" + bundle.Version + "/" + bundlePath,
                    Sources = sources.Select(sr => sr.PathOrUrl).ToList()
                }
            };


            foreach (var option in bundleOption)
            {
                foreach (var source in option.Sources)
                {
                    AuthorizeManager.AuthorizeActionOnPath(source, ActionKey.ReadFromDisk);
                }
            }



            _bundleManager.AddBundle(bundleOption);

            var path       = "";
            var bundleNmae = "~/BrowsersCodeOutPut/" + bundle.Guid + "/" + bundle.Version + "/" + bundlePath
                             .Replace(".", "-");
            var    url = bundleNmae.Replace("~", localHost);
            string contents;

            using (var wc = new System.Net.WebClient())
            {
                wc.Encoding = Encoding.UTF8;
                contents    = wc.DownloadString(url);
            }

            if (bundlePath.IndexOf(".css", StringComparison.Ordinal) > -1)
            {
                path = Config.StyleDebugPath + (bundlePath[0] == '/' ? bundlePath.Substring(1) : bundlePath);
            }
            else
            {
                path = Config.ScriptDebugPath + (bundlePath[0] == '/' ? bundlePath.Substring(1) : bundlePath);
            }
            FileSystemManager.CreatDirectoryIfNotExist(AuthorizeManager.AuthorizeActionOnPath(path.Substring(0, path.LastIndexOf("/", StringComparison.Ordinal)), ActionKey.WriteToDisk));
            await WriteFileAsync(path,
                                 "", "", contents);

            if (isPublish)
            {
                var minContent = "";
                if (bundlePath.IndexOf(".css", StringComparison.Ordinal) > -1)
                {
                    path = Config.StyleDistPath + (bundlePath[0] == '/' ? bundlePath.Substring(1) : bundlePath);

                    minContent = _compressManager.CompressCss(contents);
                }
                else
                {
                    path = Config.ScriptDistPath + (bundlePath[0] == '/' ? bundlePath.Substring(1) : bundlePath);


                    minContent = _compressManager.CompressJavaScript(contents, path);
                }
                FileSystemManager.CreatDirectoryIfNotExist(AuthorizeManager.AuthorizeActionOnPath(path.Substring(0, path.LastIndexOf("/", StringComparison.Ordinal)), ActionKey.WriteToDisk));
                await WriteFileAsync(path,
                                     "", "", minContent);
            }

            _bundleManager.RemoveBundle(bundleNmae);

            BrowsersCodeInfo bundleInfo = null;

            var bundleInfoCache = CacheManager.Get <BrowsersCodeInfo>(CacheManager.GetBrowsersCodeInfoKey(CacheKey.BrowsersCodeInfo.ToString(),
                                                                                                          bundle.PathOrUrl));

            if (bundleInfoCache.IsCached)
            {
                bundleInfo = bundleInfoCache.Value;
            }



            //var bundleInfo = KS.Core.CodeManager.SourceControl.BrowsersCodeInfos.FirstOrDefault(bc => bc.BundleUrl == bundle.PathOrUrl);

            if (bundleInfo != null)
            {
                bundleInfo.Version = bundle.Version.ToString();

                CacheManager.StoreForEver(CacheManager.GetBrowsersCodeInfoKey(CacheKey.BrowsersCodeInfo.ToString(),
                                                                              bundle.PathOrUrl), bundleInfo);
            }

            if (bundle.Code != BundleCode + bundle.Id)
            {
                await SourceControl.AddOrUpdateDependencyEngineAsync(new BundleDependency()
                {
                    DependencyKey = bundle.Code,
                    Path          = bundle.PathOrUrl,
                    Dependency    = await GetBundleDependencyForDependencyEngieen(bundle.Id),
                    Version       = bundle.Version,
                    IsPublish     = isPublish,
                    IsDelete      = false
                });
            }


            return(true);
        }