示例#1
0
        private void DavMKCol_ProcessDavRequest(object sender, EventArgs e)
        {
            if (HttpContext.Current == null)
            {
                base.AbortRequest(ServerResponseCode.BadRequest);
            }
            else
            {
                FileItem item = Directory.GetItem(VirtualDirectory.PathInfo(SolutionName));

                if (item == null)
                {
                    base.AbortRequest(ServerResponseCode.BadRequest);
                    return;
                }

                if (item.AccessRights.Equals("r"))
                {
                    base.AbortRequest(DavMKColResponseCode.MethodNotAllowed);
                    return;
                }

                //Check to see if the RequestPath is already a resource


                if (Directory._fileSystem.DirectoryExists(item.RelativePath))
                {
                    base.AbortRequest(DavMKColResponseCode.MethodNotAllowed);
                }
                else
                {
                    //The parent folder does not exist
                    if (!Directory._fileSystem.DirectoryExists(System.IO.Path.GetDirectoryName(item.RelativePath)))
                    {
                        base.AbortRequest(DavMKColResponseCode.Conflict);
                    }
                    else
                    {
                        try
                        {
                            //Create the subFolder
                            Directory._fileSystem.CreateSubDirectory(System.IO.Path.GetDirectoryName(item.RelativePath), System.IO.Path.GetFileName(item.RelativePath));
                        }
                        catch (Exception)
                        {
                            base.AbortRequest(DavMKColResponseCode.Forbidden);
                        }
                    }
                }
            }
        }
示例#2
0
        private void DavDelete_ProcessDavRequest(object sender, EventArgs e)
        {
            if (HttpContext.Current == null)
            {
                base.AbortRequest(ServerResponseCode.BadRequest);
            }
            else
            {
                //Check to see if the resource is a folder

                FileItem item = Directory.GetItem(VirtualDirectory.PathInfo(SolutionName));
                if (item == null)
                {
                    base.AbortRequest(ServerResponseCode.BadRequest);
                    return;
                }

                if (item.AccessRights.Equals("r"))
                {
                    base.AbortRequest(403);
                    return;
                }

                if (Directory._fileSystem.DirectoryExists(item.RelativePath))
                {
                    try
                    {
                        Directory._fileSystem.DeleteDirectory(item.RelativePath);
                    }
                    catch (Exception) {
                        base.AbortRequest(DavDeleteResponseCode.Locked);
                        return;
                    }
                }
                else
                {
                    try
                    {
                        Directory._fileSystem.DeleteFile(item.RelativePath);
                    }
                    catch (Exception)
                    {
                        base.AbortRequest(DavDeleteResponseCode.Locked);
                        return;
                    }
                }
            }
        }
示例#3
0
        private void DavPut_ProcessDavRequest(object sender, EventArgs e)
        {
            if (HttpContext.Current == null)
            {
                base.AbortRequest(ServerResponseCode.BadRequest);
            }
            else
            {
                FileItem item = Directory.GetItem(VirtualDirectory.PathInfo(SolutionName));
                if (item == null)
                {
                    base.AbortRequest(ServerResponseCode.BadRequest);
                    return;
                }

                if (item.AccessRights.Equals("r"))
                {
                    base.AbortRequest(ServerResponseCode.BadRequest);
                    return;
                }

                if (item.RelativePath.EndsWith(@"\mkdir"))
                {
                    String[] arr            = item.RelativePath.Split(new String[] { "\\" }, StringSplitOptions.RemoveEmptyEntries);
                    String   folderToCreate = arr[arr.Length - 2].Trim();
                    if (!String.IsNullOrEmpty(folderToCreate))
                    {
                        String rp = "";
                        for (int i = 0; i < arr.Length - 2; i++)
                        {
                            rp = rp + arr[i].Trim() + "\\";
                        }

                        if (!Directory._fileSystem.DirectoryExists(rp + folderToCreate))
                        {
                            Directory._fileSystem.CreateSubDirectory(rp, folderToCreate);
                        }
                    }
                    return;
                }

                //The parent folder does not exist
                if (!Directory._fileSystem.DirectoryExists(System.IO.Path.GetDirectoryName(item.RelativePath)))
                {
                    base.AbortRequest(ServerResponseCode.NotFound);
                }
                else
                {
                    if (!base.OverwriteExistingResource)
                    {
                        //Check to see if the resource already exists
                        if (System.IO.File.Exists(item.RelativePath))
                        {
                            base.AbortRequest(DavPutResponseCode.Conflict);
                        }
                        else
                        {
                            SaveFile(item);
                        }
                    }
                    else
                    {
                        SaveFile(item);
                    }
                }
            }
        }
示例#4
0
        private void FileWebDAVModule_BasicAuthorization(object sender, BasicAuthorizationArgs e)
        {
            e.Authorized = false;
            if (!String.IsNullOrEmpty(e.Realm))
            {
                if (!string.IsNullOrEmpty(e.UserName) && !string.IsNullOrEmpty(e.Password))
                {
                    if (e.UserName.ToLower().Equals("root"))
                    {
                        e.Authorized = Common.Solution.CreateFromContext(e.Realm).SolutionPassword.Equals(e.Password);
                        if (e.Authorized)
                        {
                            CreateFileSystemFoldersIfNotExists(e.Realm);
                            VirtualDirectory vdir = new VirtualDirectory(_fileSystem, e.Realm) { UserName = e.UserName };

                            String solutionFolder = String.Format(@"{0}", e.Realm);
                            vdir.AddFolder("access", System.IO.Path.Combine(solutionFolder, "access"), "r");
                            vdir.AddFolder("filesystem", System.IO.Path.Combine(solutionFolder, "filesystem"), "r");
                            vdir.AddFolder("log", System.IO.Path.Combine(solutionFolder, "log"), "r");
                            vdir.AddFolder("resource", System.IO.Path.Combine(solutionFolder, "resource"), "r");

                            e.UserData = vdir;
                        }
                        return;
                    }

                    if (e.UserName.ToLower().Equals("admin"))
                    {
                        e.Authorized = Common.Solution.CreateFromContext(e.Realm).SolutionPassword.Equals(e.Password);
                        if (e.Authorized)
                        {
                            CreateFileSystemFoldersIfNotExists(e.Realm);
                            VirtualDirectory vdir = new VirtualDirectory(_fileSystem, e.Realm) { UserName = e.UserName };

                            String solutionFolder = String.Format(@"{0}\filesystem", e.Realm);
                            vdir.AddFolder("shared", String.Format(@"{0}\shared", solutionFolder), "rw");
                            vdir.AddFolder("private", String.Format(@"{0}\private", solutionFolder), "rw");
                            vdir.AddFolder("exchange", String.Format(@"{0}\exchange", solutionFolder), "rw");

                            vdir.AddVirtualFile("exchange.txt", String.Format(@"{0}\exchange", solutionFolder), new CatalogFile());
                            vdir.AddVirtualFile("private.txt", String.Format(@"{0}\private", solutionFolder), new CatalogFile());
                            vdir.AddVirtualFile("shared.txt", String.Format(@"{0}\shared", solutionFolder), new CatalogFile());
                            vdir.AddVirtualFile("log.txt", String.Format(@"{0}\log", solutionFolder), new CatalogFile());

                            e.UserData = vdir;
                        }
                    return;
                    }

                    //user
                    Guid userId;
                    if (System.Guid.TryParse(e.UserName, out userId))
                    {
                        e.Authorized = Common.Logon.UserExists(e.Realm, userId, e.Password);
                        if (e.Authorized)
                        {
                            CreateFileSystemFoldersIfNotExists(e.Realm);
                            VirtualDirectory vdir = new VirtualDirectory(_fileSystem, e.Realm) { UserName = e.UserName };

                            String solutionFolder = String.Format(@"{0}\filesystem", e.Realm);

                            if (!_fileSystem.DirectoryExists(String.Format(@"{0}\private\{1}", solutionFolder, e.UserName)))
                                _fileSystem.CreateSubDirectory(String.Format(@"{0}\private", solutionFolder), e.UserName);
                            if (!_fileSystem.DirectoryExists(String.Format(@"{0}\log\{1}", solutionFolder, e.UserName)))
                                _fileSystem.CreateSubDirectory(String.Format(@"{0}\log", solutionFolder), e.UserName);
                            
                            vdir.AddFolder("shared", String.Format(@"{0}\shared", solutionFolder), "r");
                            vdir.AddFolder("private", String.Format(@"{0}\private\{1}", solutionFolder, e.UserName), "rw");
                            vdir.AddFolder("log", String.Format(@"{0}\log\{1}", solutionFolder, e.UserName), "rw");

                            if(Common.Logon.CheckIfFilterShared(e.Realm))
                                vdir.AddVirtualFile("shared.txt", String.Format(@"{0}\shared", solutionFolder), new CatalogFileFiltered(Common.Solution.CreateFromContext(e.Realm), userId));
                            else
                                vdir.AddVirtualFile("shared.txt", String.Format(@"{0}\shared", solutionFolder), new CatalogFile());

                            vdir.AddVirtualFile("private.txt", String.Format(@"{0}\private\{1}", solutionFolder, e.UserName), new CatalogFile());
                            vdir.AddVirtualFile("log.txt", String.Format(@"{0}\log\{1}", solutionFolder, e.UserName), new CatalogFile());

                            e.UserData = vdir;
                        }
                    }

                }
            }
        }
示例#5
0
        private void DavPropFind_ProcessDavRequest(object sender, EventArgs e)
        {
            if (HttpContext.Current == null)
            {
                base.AbortRequest(ServerResponseCode.BadRequest);
            }
            else
            {
                String _path     = VirtualDirectory.PathInfo(SolutionName);
                String _basePath = HttpContext.Current.Request.Url.AbsoluteUri;

                if (!Path.HasExtension(_basePath) && !_basePath.EndsWith("/"))
                {
                    _basePath += "/";
                }

                FileItem item = Directory.GetItem(_path);


                if ((!string.IsNullOrEmpty(_path)) && (!_path.Equals("/")) && (item == null))  // no such item
                {
                    base.AbortRequest(ServerResponseCode.NotFound);
                    return;
                }


                if (item != null)
                {
                    if ((item is VirtualFile) || (Directory._fileSystem.FileExists(item.RelativePath))) // file
                    {
                        if (item is VirtualFile)
                        {
                            DavFile _davFile = new DavFile(item.Name, _basePath);
                            _davFile.CreationDate = DateTime.Now.ToUniversalTime();
                            _davFile.LastModified = DateTime.Now.ToUniversalTime();

                            _davFile.SupportsExclusiveLock = true;
                            _davFile.SupportsSharedLock    = true;
                            _davFile.ContentLength         = 0;

                            base.FileResources.Add(_davFile);
                            return;
                        }

                        FileSystem.ItemInfo _fileInfo = Directory._fileSystem.GetFileInfo(item.RelativePath);

                        if (_fileInfo != null)
                        {
                            DavFile _davFile = new DavFile(_fileInfo.Name, _basePath);
                            _davFile.CreationDate = _fileInfo.CreationTime;
                            _davFile.LastModified = _fileInfo.LastWriteTime;

                            _davFile.SupportsExclusiveLock = true;
                            _davFile.SupportsSharedLock    = true;
                            _davFile.ContentLength         = (int)_fileInfo.Length;

                            base.FileResources.Add(_davFile);
                            return;
                        }
                    }
                }
                //dir
                DavFolder           _rootResource;
                FileSystem.ItemInfo _dirInfo;
                if (item == null)
                {
                    _rootResource = new DavFolder("DavWWWRoot", _basePath);
                    _dirInfo      = Directory._fileSystem.GetDirectoryInfo("");
                }
                else
                {
                    _dirInfo      = Directory._fileSystem.GetDirectoryInfo(item.RelativePath);
                    _rootResource = new DavFolder(_dirInfo.Name, _basePath);
                }
                _rootResource.CreationDate  = _dirInfo.CreationTime;
                _rootResource.LastModified  = _dirInfo.LastWriteTime;
                _rootResource.ContentLength = (int)_dirInfo.Length;

                base.CollectionResources.Add(_rootResource);


                //TODO: Only populate the requested properties
                switch (base.RequestDepth)
                {
                case Sphorium.WebDAV.Server.Framework.DepthType.ResourceOnly:
                    break;

                default:

                    if (item == null)     //root dir
                    {
                        foreach (FileItem subItem in Directory.items)
                        {
                            if (subItem is VirtualFile)
                            {
                                DavFile _davFile = new DavFile(subItem.Name, _basePath + subItem.Name);
                                _davFile.CreationDate = DateTime.Now.ToUniversalTime();
                                _davFile.LastModified = DateTime.Now.ToUniversalTime();

                                _davFile.SupportsExclusiveLock = true;
                                _davFile.SupportsSharedLock    = true;
                                _davFile.ContentLength         = 0;

                                base.FileResources.Add(_davFile);
                            }
                            else
                            {
                                DavFolder           _davFolder = new DavFolder(subItem.Name, _basePath + subItem.Name);
                                FileSystem.ItemInfo _subDir    = Directory._fileSystem.GetDirectoryInfo(subItem.RelativePath);

                                _davFolder.CreationDate  = _subDir.CreationTime;
                                _davFolder.LastModified  = _subDir.LastWriteTime;
                                _davFolder.ContentLength = (int)_subDir.Length;
                                base.CollectionResources.Add(_davFolder);
                            }
                        }
                    }
                    else     // non root dir
                    {
                        foreach (FileSystem.ItemInfo _subDir in Directory._fileSystem.EnumerateDirectories(item.RelativePath))
                        {
                            //TODO: Only populate the requested properties
                            DavFolder _davFolder = new DavFolder(_subDir.Name, _basePath + _subDir.Name);
                            _davFolder.CreationDate  = _subDir.CreationTime;
                            _davFolder.LastModified  = _subDir.LastWriteTime;
                            _davFolder.ContentLength = (int)_subDir.Length;

                            base.CollectionResources.Add(_davFolder);
                        }

                        foreach (FileSystem.ItemInfo _fileInfo in Directory._fileSystem.EnumerateFiles(item.RelativePath))
                        {
                            //TODO: Only populate the requested properties
                            DavFile _davFile = new DavFile(_fileInfo.Name, _basePath + _fileInfo.Name);
                            _davFile.CreationDate = _fileInfo.CreationTime;
                            _davFile.LastModified = _fileInfo.LastWriteTime;

                            _davFile.SupportsExclusiveLock = true;
                            _davFile.SupportsSharedLock    = true;
                            _davFile.ContentLength         = (int)_fileInfo.Length;



                            base.FileResources.Add(_davFile);
                        }
                    }
                    break;
                }
            }
        }
示例#6
0
        private void FileWebDAVModule_BasicAuthorization(object sender, BasicAuthorizationArgs e)
        {
            e.Authorized = false;
            if (!String.IsNullOrEmpty(e.Realm))
            {
                if (!string.IsNullOrEmpty(e.UserName) && !string.IsNullOrEmpty(e.Password))
                {
                    if (e.UserName.ToLower().Equals("root"))
                    {
                        e.Authorized = Common.Solution.CreateFromContext(e.Realm).SolutionPassword.Equals(e.Password);
                        if (e.Authorized)
                        {
                            CreateFileSystemFoldersIfNotExists(e.Realm);
                            VirtualDirectory vdir = new VirtualDirectory(_fileSystem, e.Realm)
                            {
                                UserName = e.UserName
                            };

                            String solutionFolder = String.Format(@"{0}", e.Realm);
                            vdir.AddFolder("access", System.IO.Path.Combine(solutionFolder, "access"), "r");
                            vdir.AddFolder("filesystem", System.IO.Path.Combine(solutionFolder, "filesystem"), "r");
                            vdir.AddFolder("log", System.IO.Path.Combine(solutionFolder, "log"), "r");
                            vdir.AddFolder("resource", System.IO.Path.Combine(solutionFolder, "resource"), "r");

                            e.UserData = vdir;
                        }
                        return;
                    }

                    if (e.UserName.ToLower().Equals("admin"))
                    {
                        e.Authorized = Common.Solution.CreateFromContext(e.Realm).SolutionPassword.Equals(e.Password);
                        if (e.Authorized)
                        {
                            CreateFileSystemFoldersIfNotExists(e.Realm);
                            VirtualDirectory vdir = new VirtualDirectory(_fileSystem, e.Realm)
                            {
                                UserName = e.UserName
                            };

                            String solutionFolder = String.Format(@"{0}\filesystem", e.Realm);
                            vdir.AddFolder("shared", String.Format(@"{0}\shared", solutionFolder), "rw");
                            vdir.AddFolder("private", String.Format(@"{0}\private", solutionFolder), "rw");
                            vdir.AddFolder("exchange", String.Format(@"{0}\exchange", solutionFolder), "rw");

                            vdir.AddVirtualFile("exchange.txt", String.Format(@"{0}\exchange", solutionFolder), new CatalogFile());
                            vdir.AddVirtualFile("private.txt", String.Format(@"{0}\private", solutionFolder), new CatalogFile());
                            vdir.AddVirtualFile("shared.txt", String.Format(@"{0}\shared", solutionFolder), new CatalogFile());
                            vdir.AddVirtualFile("log.txt", String.Format(@"{0}\log", solutionFolder), new CatalogFile());

                            e.UserData = vdir;
                        }
                        return;
                    }

                    //user
                    Guid userId;
                    if (System.Guid.TryParse(e.UserName, out userId))
                    {
                        e.Authorized = Common.Logon.UserExists(e.Realm, userId, e.Password);
                        if (e.Authorized)
                        {
                            CreateFileSystemFoldersIfNotExists(e.Realm);
                            VirtualDirectory vdir = new VirtualDirectory(_fileSystem, e.Realm)
                            {
                                UserName = e.UserName
                            };

                            String solutionFolder = String.Format(@"{0}\filesystem", e.Realm);

                            if (!_fileSystem.DirectoryExists(String.Format(@"{0}\private\{1}", solutionFolder, e.UserName)))
                            {
                                _fileSystem.CreateSubDirectory(String.Format(@"{0}\private", solutionFolder), e.UserName);
                            }
                            if (!_fileSystem.DirectoryExists(String.Format(@"{0}\log\{1}", solutionFolder, e.UserName)))
                            {
                                _fileSystem.CreateSubDirectory(String.Format(@"{0}\log", solutionFolder), e.UserName);
                            }

                            vdir.AddFolder("shared", String.Format(@"{0}\shared", solutionFolder), "r");
                            vdir.AddFolder("private", String.Format(@"{0}\private\{1}", solutionFolder, e.UserName), "rw");
                            vdir.AddFolder("log", String.Format(@"{0}\log\{1}", solutionFolder, e.UserName), "rw");

                            if (Common.Logon.CheckIfFilterShared(e.Realm))
                            {
                                vdir.AddVirtualFile("shared.txt", String.Format(@"{0}\shared", solutionFolder), new CatalogFileFiltered(Common.Solution.CreateFromContext(e.Realm), userId));
                            }
                            else
                            {
                                vdir.AddVirtualFile("shared.txt", String.Format(@"{0}\shared", solutionFolder), new CatalogFile());
                            }

                            vdir.AddVirtualFile("private.txt", String.Format(@"{0}\private\{1}", solutionFolder, e.UserName), new CatalogFile());
                            vdir.AddVirtualFile("log.txt", String.Format(@"{0}\log\{1}", solutionFolder, e.UserName), new CatalogFile());

                            e.UserData = vdir;
                        }
                    }
                }
            }
        }
示例#7
0
        private void DavGet_ProcessDavRequest(object sender, EventArgs e)
        {
            if (HttpContext.Current == null)
            {
                base.AbortRequest(ServerResponseCode.BadRequest);
            }
            else
            {
                FileItem item = Directory.GetItem(VirtualDirectory.PathInfo(SolutionName));

                if (item == null)
                {
                    base.AbortRequest(ServerResponseCode.BadRequest);
                }
                else
                {
                    if (item.RelativePath.EndsWith(@"\mkdir"))
                    {
                        String[] arr            = item.RelativePath.Split(new String[] { "\\" }, StringSplitOptions.RemoveEmptyEntries);
                        String   folderToCreate = arr[arr.Length - 2].Trim();
                        if (!String.IsNullOrEmpty(folderToCreate))
                        {
                            String rp = "";
                            for (int i = 0; i < arr.Length - 2; i++)
                            {
                                rp = rp + arr[i].Trim() + "\\";
                            }

                            if (!Directory._fileSystem.DirectoryExists(rp + folderToCreate))
                            {
                                Directory._fileSystem.CreateSubDirectory(rp, folderToCreate);
                            }
                        }

                        base.ResponseOutput = System.Text.ASCIIEncoding.ASCII.GetBytes("ok");
                        return;
                    }

                    if (item is VirtualFile)
                    {
                        ((VirtualFile)item).GetData(HttpContext.Current.Response.OutputStream);
                        base.ResponseOutput = new byte[0];
                    }
                    else
                    {
                        if (!Directory._fileSystem.FileExists(item.RelativePath))
                        {
                            base.AbortRequest(ServerResponseCode.NotFound);
                        }
                        else
                        {
                            using (Stream _fileStream = Directory._fileSystem.OpenRead(item.RelativePath))
                            {
                                Stream output = ImageResizer.TryResize(Directory._fileSystem.GetFileInfo2(item.RelativePath).Name, _fileStream, HttpContext.Current.Request.QueryString);

                                long   _fileSize      = output.Length;
                                byte[] _responseBytes = new byte[_fileSize];

                                output.Read(_responseBytes, 0, (int)_fileSize);

                                base.ResponseOutput = _responseBytes;

                                output.Close();
                            }
                        }
                    }
                }
            }
        }