Esempio n. 1
0
        private void ProcessRename(string path, HttpCall call)
        {
            if (readOnly)
            {
                call.Write("KO");
                return;
            }
            char[] forbiddenTo = { '/', '\\' };
            string to;

            if (!call.Parameters.TryGetValue("to", out to))
            {
                call.Write("KO (missing to)");
                return;
            }
            if (to.IndexOfAny(forbiddenTo) >= 0 || to == "." || to == "..")
            {
                call.Write("KO (forbidden)");
                return;
            }

            VFSItem fil = vfs.GetItem(path);

            if (fil != null)
            {
                call.Write(fil.RenameTo(to) ? "OK" : "KO");
            }
            else
            {
                call.Write("KO");
            }
        }
Esempio n. 2
0
        private void ProcessUpload(string path, HttpCall call)
        {
            if (readOnly)
            {
                call.Write("KO");
                return;
            }
            string soffset = call.Request.Headers["X-Sunfish-Offset"];
            string slength = call.Request.Headers["X-Sunfish-Length"];

            if (string.IsNullOrEmpty(soffset))
            {
                call.Parameters.TryGetValue("offset", out soffset);
            }
            if (string.IsNullOrEmpty(slength))
            {
                call.Parameters.TryGetValue("length", out slength);
            }
            int pos, len;

            int.TryParse(soffset, out pos);
            int.TryParse(slength, out len);
            try
            {
                VFSItem fil = vfs.GetItem(path);
                if (fil == null)
                {
                    fil = vfs.Create(path);
                }
                if (fil.Directory && len > 0)
                {
                    call.Write("KO: Exists as directory");
                    return;
                }
                if (len > 0)
                {
                    using (Stream s = fil.OpenWrite())
                    {
                        s.Position = pos;
                        using (Stream sin = call.Request.InputStream)
                        {
                            s.TransferFrom(sin, len);
                        }
                    }
                }
                call.Write("OK");
            }
            catch (Exception e)
            {
                call.Write("KO: " + e.GetType().Name + ":" + e.Message);
            }
        }
Esempio n. 3
0
 private void ProcessGET(string path, HttpCall call)
 {
     if (path.EndsWith("/"))
     {
         VFSItem idx = vfs.GetItem(path + index);
         //Directory entry, go for index file or navigation
         if (index != null)
         {
             if (idx != null && !idx.Directory)
             {
                 DownloadAt(idx, call);
                 return;
             }
         }
         if (allowNavigation)
         {
             //TODO: Block subfolder navigation if not allowed checking path
             idx = vfs.GetItem(path);
             if (idx != null && idx.Directory)
             {
                 WriteIndex(path, idx, call);
             }
             else
             {
                 call.HTTPNotFound();
             }
         }
         else
         {
             call.HTTPForbidden();
         }
     }
     else
     {
         VFSItem idx = vfs.GetItem(path);
         if (idx != null)
         {
             if (idx.Directory)
             {
                 call.Redirect(call.Request.Url.LocalPath + "/");
             }
             else
             {
                 DownloadAt(idx, call);
             }
         }
         else
         {
             call.HTTPNotFound();
         }
     }
 }
Esempio n. 4
0
 private void DownloadAt(VFSItem item, HttpCall call)
 {
     using (Stream s = item.OpenRead())
     {
         if (s == null)
         {
             Error500(call, "Problem transfering file");
             return;
         }
         call.Response.ContentType = MimeTypes.GetMimeType(Path.GetExtension(item.Name));
         call.Out.BaseStream.TransferFrom(s);
     }
 }
Esempio n. 5
0
        private void ProcessDelete(string path, HttpCall call)
        {
            VFSItem fil = !readOnly && allowDelete?vfs.GetItem(path) : null;

            if (fil != null)
            {
                call.Write(fil.Delete() ? "OK" : "KO");
            }
            else
            {
                call.Write("KO");
            }
        }
Esempio n. 6
0
        private void ProcessOpen(string path, HttpCall call)
        {
            if (!allowExec)
            {
                call.Write("KO");
                return;
            }
            VFSItem fil = vfs.GetItem(path);

            if (fil != null)
            {
                string fpath = ((VFSFolderFileSystem)fil.Folder).GetFSPath(fil.Path);
                System.Diagnostics.Process.Start(fpath);
                call.Write("OK");
            }
            else
            {
                call.Write("KO");
            }
        }
Esempio n. 7
0
        private void ProcessIcon(string path, HttpCall call)
        {
            if (path.EndsWith("/"))
            {
                call.HTTPForbidden();
            }
            else
            {
                VFSItem   fil  = vfs.GetItem(path);
                VFSFolder fold = fil.Folder;

                try
                {
                    if (fil.Length < 10485760) //10Mb
                    {
                        using (Stream fstream = fil.OpenRead())
                            using (Image i = Image.FromStream(fstream))
                                using (Image t = i.GetThumbnailImage(32, 32, null, IntPtr.Zero))
                                {
                                    WritePNG((Bitmap)t, call);
                                    return;
                                }
                    }
                }
                catch { };

                if (fold is VFSFolderFileSystem)
                {
                    string realpath = ((VFSFolderFileSystem)fold).GetFSPath(fil.Path);
                    using (ShellIcon i = new ShellIcon(realpath))
                        WritePNG(i.Image, call);
                }
                else
                {
                    using (ShellIcon i = new ShellIcon(path))
                        WritePNG(i.Image, call);
                }
            }
        }
Esempio n. 8
0
        private void ProcessFolderZip(string path, HttpCall call)
        {
            VFSItem fil = vfs.GetItem(path);

            if (fil != null)
            {
                using (ZipDownload z = new ZipDownload(call))
                {
                    if (fil.Directory)
                    {
                        z.AddDirectory(fil, null);
                    }
                    else
                    {
                        z.AddFile(fil, fil.Name);
                    }
                }
            }
            else
            {
                call.HTTPNotFound();
            }
        }
Esempio n. 9
0
        private void WriteIndex(string path, VFSItem dir, HttpCall call)
        {
            List <WebUILink> items    = new List <WebUILink>();
            List <string>    fileList = new List <string>();

            foreach (VFSItem vd in dir.ListDirectories())
            {
                items.Add(new WebUILink()
                {
                    Icon        = "/$sunfish/folder.png",
                    Name        = vd.Name,
                    Description = "Directory",
                    Link        = vd.Name + "/",
                    Style       = "directory",
                    Actions     = new WebUILink[] {
                        new WebUILink()
                        {
                            Icon    = "archive",
                            Tooltip = "Download as zip",
                            //Click="sunfish.openFile(this)"
                            Link = vd.Name + "?action=zip"
                        },
                        readOnly?null:new WebUILink()
                        {
                            Icon    = "drive_file_rename_outline",
                            Tooltip = "Rename",
                            Click   = "sunfish.renameFile(this)"
                        },
                        allowDelete?new WebUILink()
                        {
                            Icon    = "delete",
                            Tooltip = "Delete folder",
                            Click   = "sunfish.deleteFile(this);"
                        }:null
                    }
                });
            }
            fileList.Clear();
            foreach (VFSItem vf in dir.ListFiles())
            {
                items.Add(new WebUILink()
                {
                    Icon        = vf.Name + "?action=icon",
                    Name        = vf.Name,
                    Description = "<span class='size'>" + vf.Length.ToSize() + "</span> <span class='info'>(" + MimeTypes.GetMimeType(Path.GetExtension(vf.Name)) + ")</span>",
                    Link        = vf.Name,
                    Actions     = new WebUILink[] {
                        allowExec?new WebUILink()
                        {
                            Icon    = "api",
                            Tooltip = "Open in server",
                            Click   = "sunfish.openFile(this)"
                        }:null,
                        readOnly?null:new WebUILink()
                        {
                            Icon    = "drive_file_rename_outline",
                            Tooltip = "Rename",
                            Click   = "sunfish.renameFile(this)"
                        },
                        allowDelete?new WebUILink()
                        {
                            Icon    = "delete",
                            Tooltip = "Delete file",
                            Click   = "sunfish.deleteFile(this)"
                        }:null
                    }
                });;
            }
            Dictionary <string, object> data = new Dictionary <string, object>();

            data["Breadcrumb"] = GetBreadcrumb(path);
            data["Actions"]    = new WebUILink[] {
                readOnly?null : new WebUILink()
                {
                    Icon    = "create_new_folder",
                    Tooltip = "New folder",
                    Click   = "sunfish.newFolder(this)",
                },
                readOnly?null : new WebUILink()
                {
                    Icon    = "note_add",
                    Tooltip = "New file",
                    Click   = "sunfish.newFile(this)",
                },
                readOnly?null : new WebUILink()
                {
                    Icon    = "upload",
                    Tooltip = "Upload. Drop files or fonders here",
                    Click   = "sunfish.uploadFile(this)",
                    //Style="upload-drop",
                }
            };
            data["Items"]   = items;
            data["Include"] = "<script src=\"/$sunfish/sunfish-directory.js\"></script>";
            WebUI.WriteTemplate("directory-index", call, data);
        }