Beispiel #1
0
    protected string RenderFileInfo(IFileObject file)
    {
        var    content = file.GetContent();
        string size    = VfsHelper.FormatFileSize(content.Size);

        return(String.Format("<div class=\"fileinfo date\">{1:d}</div><div class=\"fileinfo size\">{0}</div>",
                             size, content.LastModifiedTime));
    }
Beispiel #2
0
 public static bool IsImageFile(this Control ctrl, string vfsName)
 {
     return(VfsHelper.IsImageFile(vfsName));
 }
Beispiel #3
0
 public static string GetFileDownloadUrl(this Control ctrl, string fileSystemName, string vfsName)
 {
     return(VfsHelper.GetFileDownloadUrl(fileSystemName, vfsName));
 }
Beispiel #4
0
    protected void RenderFile(StringBuilder sb, IFileObject file, bool renderChildren, bool renderFile, bool extraInfo, string filesystemName)
    {
        var filePath = file.Name.Replace("\\", "/");
        var fileName = Path.GetFileName(file.Name);

        if (fileName == String.Empty)
        {
            fileName = "Root";
        }
        switch (file.Type)
        {
        case FileType.File:
            var ext = Path.GetExtension(file.Name);
            if (ext != null && ext.Length > 1)
            {
                ext = ext.Substring(1);
            }
            if (renderFile)
            {
                sb.AppendFormat("<li class=\"file ext_{0} {4}\">{3}<a class='file' href=\"javascript:void(0)\" rel=\"{1}\" filename=\"{2}\" fileurl=\"{5}\"><span class='name'>{2}</span></a></li>",
                                ext, filePath, fileName,
                                extraInfo ? RenderFileInfo(file) : "",
                                extraInfo ? "fileInfo" : "",
                                HttpUtility.HtmlAttributeEncode(VfsHelper.GetFileFullUrl(filesystemName, filePath)));
            }
            break;

        case FileType.Folder:
            if (renderFile)
            {
                sb.AppendFormat("<li class=\"directory {2}\"><a class='directory' href=\"#\" rel=\"{0}/\" filename=\"{1}\"><span class='name'>{1}</span></a>",
                                filePath, fileName, (filePath != "" ? (renderChildren ? "expanded" : "collapsed") : ""));
            }
            if (renderChildren)
            {
                sb.Append("<ul class=\"jqueryFileTree\" style=\"display: none;\">");
                var folders     = new List <IFileObject>();
                var files       = new List <IFileObject>();
                var folderFiles = file.GetChildren();
                Array.Sort(folderFiles, delegate(IFileObject x, IFileObject y) { return(x.Name.CompareTo(y.Name)); });
                foreach (var f in folderFiles)
                {
                    if (f.Type == FileType.Folder)
                    {
                        folders.Add(f);
                    }
                    else
                    {
                        files.Add(f);
                    }
                }
                foreach (var f in folders)
                {
                    RenderFile(sb, f, false, true, extraInfo, filesystemName);
                }
                foreach (var f in files)
                {
                    RenderFile(sb, f, false, true, extraInfo, filesystemName);
                }
                sb.Append("</ul>");
            }
            sb.Append("</li>");
            break;
        }
    }
Beispiel #5
0
    protected void HandleUpload(HttpRequest Request, HttpResponse Response, string filesystem)
    {
        var fs           = WebManager.GetService <IFileSystem>(filesystem);
        var result       = new List <IFileObject>();
        var resultFormat = Request["resultFormat"] ?? "text";

        for (int i = 0; i < Request.Files.Count; i++)
        {
            var file = Request.Files[i];

            // skip files with empty name; such a things happends sometimes =\
            if (String.IsNullOrEmpty(file.FileName.Trim()))
            {
                continue;
            }

            var allowedExtensions = AssertHelper.IsFuzzyEmpty(Request["allowedextensions"]) ? null : JsHelper.FromJsonString <IList <string> >(HttpUtility.UrlDecode(Request["allowedextensions"]));
            var originalFileName  = Path.GetFileName(file.FileName);
            var fileName          = Request["dir"] != null && Request["dir"] != "" && Request["dir"] != "/" ? Path.Combine(Request["dir"], originalFileName) : originalFileName;
            log.Write(LogEvent.Debug, "Uploading - file name: {0}", fileName);
            if ((allowedExtensions != null && allowedExtensions.IndexOf(Path.GetExtension(fileName).ToLower()) < 0) || Array.IndexOf(blockedExtensions, Path.GetExtension(fileName).ToLower()) >= 0)
            {
                throw new Exception(WebManager.GetLabel(FileTreeAjaxHandler.InvalidFileTypeMessage));
            }

            var uploadFile    = fs.ResolveFile(fileName);
            var uploadPNGFile = fs.ResolveFile(fileName + ".png");             // additional checking of resized images if file names are similar
            if ((uploadFile.Exists() || uploadPNGFile.Exists()) && Request["overwrite"] != null && !Convert.ToBoolean(Request["overwrite"]))
            {
                int fileNum = 0;
                do
                {
                    fileNum++;
                    var extIdx      = fileName.LastIndexOf('.');
                    var newFileName = extIdx >= 0 ? String.Format("{0}{1}{2}", fileName.Substring(0, extIdx), fileNum, fileName.Substring(extIdx)) : fileName + fileNum.ToString();
                    uploadFile = fs.ResolveFile(newFileName);
                } while (uploadFile.Exists() && fileNum < 100);
                if (uploadFile.Exists())
                {
                    var extIdx       = fileName.LastIndexOf('.');
                    var uniqueSuffix = Guid.NewGuid().ToString();
                    uploadFile = fs.ResolveFile(
                        extIdx >= 0 ?  fileName.Substring(0, extIdx) + uniqueSuffix + fileName.Substring(extIdx) : fileName + uniqueSuffix);                 // 99.(9)% new file!
                }
                fileName = uploadFile.Name;
            }
            // special handling for images
            if (Request["image"] == "compressed" || Request["imageformat"] != null || Request["image_max_width"] != null || Request["image_max_height"] != null)
            {
                uploadFile = ImageHelper.SaveAndResizeImage(
                    file.InputStream, fs, uploadFile,
                    Convert.ToInt32(Request["image_max_width"] ?? "0"),
                    Convert.ToInt32(Request["image_max_height"] ?? "0"),
                    Request["imageformat"] != null ? ImageHelper.ResolveImageFormat(Request["imageformat"]) : null
                    );
            }
            else
            {
                uploadFile.CopyFrom(file.InputStream);
            }

            result.Add(uploadFile);
        }

        switch (resultFormat)
        {
        case "text":
            Response.Write(String.Join("\n", result.Select(f => f.Name).ToArray()));
            break;

        case "json":
            Response.Write(JsHelper.ToJsonString(
                               result.Select(f =>
                                             new Dictionary <string, object> {
                { "name", Path.GetFileName(f.Name) },
                { "filepath", f.Name },
                { "size", f.GetContent().Size },
                { "url", VfsHelper.GetFileUrl(filesystem, f.Name) },
                { "full_url", VfsHelper.GetFileFullUrl(filesystem, f.Name) }
            }
                                             ).ToArray()));
            break;
        }
    }