Example #1
0
 protected virtual void RenderFileManagerResult(HttpContext context, file_manager_result result)
 {
     context.Response.ContentType = "application/x-javascript";
     context.Response.Write(JsonValue.Serialize <file_manager_result>(result));
 }
Example #2
0
        private void FileManagerRequest()
        {
            string currentPath    = string.Empty;
            string currentUrl     = string.Empty;
            string currentDirPath = string.Empty;
            string moveupDirPath  = string.Empty;
            string rootUrl;
            string dirPath;

            InitPathAndUrl(out dirPath, out rootUrl);

            string dirName = Request.QueryString["dir"];

            if (!string.IsNullOrEmpty(dirName))
            {
                if (Array.IndexOf("image,flash,media,file".Split(','), dirName) == -1)
                {
                    Response.Write("Invalid Directory name.");
                    End();
                    return;
                }
                dirPath = string.Concat(dirPath, dirName, "/");
                rootUrl = string.Concat(rootUrl, dirName, "/");
                if (!Directory.Exists(dirPath))
                {
                    Directory.CreateDirectory(dirPath);
                }
            }

            string path = Request.QueryString["path"] ?? string.Empty;

            if (string.IsNullOrEmpty(path))
            {
                currentPath    = dirPath;
                currentUrl     = rootUrl;
                currentDirPath = string.Empty;
                moveupDirPath  = string.Empty;
            }
            else
            {
                currentPath    = string.Concat(dirPath, path);
                currentUrl     = string.Concat(rootUrl, path);
                currentDirPath = path;
                moveupDirPath  = Regex.Replace(currentDirPath, @"(.*?)[^\/]+\/$", "$1");
            }

            string order = Request.QueryString["order"] ?? string.Empty;

            order = order.ToLower();

            //不允许使用..移动到上一级目录
            if (Regex.IsMatch(path, @"\.\."))
            {
                Response.Write("Access is not allowed.");
                End();
                return;
            }
            //最后一个字符不是/
            if (path != string.Empty && !path.EndsWith("/"))
            {
                Response.Write("Parameter is not valid.");
                End();
                return;
            }
            //目录不存在或不是目录
            if (!Directory.Exists(currentPath))
            {
                Response.Write("Directory does not exist.");
                End();
                return;
            }

            //遍历目录取得文件信息
            string[] dirList  = Directory.GetDirectories(currentPath);
            string[] fileList = Directory.GetFiles(currentPath);
            switch (order)
            {
            case "size":
                Array.Sort(dirList, new NameSorter());
                Array.Sort(fileList, new SizeSorter());
                break;

            case "type":
                Array.Sort(dirList, new NameSorter());
                Array.Sort(fileList, new TypeSorter());
                break;

            case "name":
            default:
                Array.Sort(dirList, new NameSorter());
                Array.Sort(fileList, new NameSorter());
                break;
            }

            file_manager_result result = new file_manager_result();

            result.moveup_dir_path  = moveupDirPath;
            result.current_dir_path = currentDirPath;
            result.current_url      = currentUrl;
            result.total_count      = dirList.Length + fileList.Length;
            for (int i = 0; i < dirList.Length; i++)
            {
                DirectoryInfo dir  = new DirectoryInfo(dirList[i]);
                file_or_dir   hash = new file_or_dir();
                hash.is_dir   = true;
                hash.has_file = (dir.GetFileSystemInfos().Length > 0);
                hash.filesize = 0;
                hash.is_photo = false;
                hash.filetype = "";
                hash.filename = dir.Name;
                hash.datetime = dir.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss");
                result.file_list.Add(hash);
            }
            for (int i = 0; i < fileList.Length; i++)
            {
                FileInfo    file = new FileInfo(fileList[i]);
                file_or_dir hash = new file_or_dir();
                hash.is_dir   = false;
                hash.has_file = false;
                hash.filesize = file.Length;
                hash.is_photo = (Array.IndexOf("png,jpg,gif,bmp,jpeg".Split(','), file.Extension.Substring(1).ToLower()) >= 0);
                hash.filetype = file.Extension.Substring(1);
                hash.filename = file.Name;
                hash.datetime = file.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss");
                result.file_list.Add(hash);
            }
            RenderFileManagerResult(Context, result);
        }