Пример #1
0
 private void RemoveThumbs(FullPath path)
 {
     if (path.Directory != null)
     {
         string thumbPath = path.Root.GetExistingThumbPath(path.Directory);
         if (thumbPath != null)
             Directory.Delete(thumbPath, true);
     }
     else
     {
         string thumbPath = path.Root.GetExistingThumbPath(path.File);
         if (thumbPath != null)
             File.Delete(thumbPath);
     }
 }
Пример #2
0
        internal ImageWithMime GenerateThumbnail(FullPath originalImage)
        {
            string name = originalImage.File.Name;
                for (int i = name.Length - 1; i >= 0; i--)
                {
                    if (name[i] == '_')
                    {
                        name = name.Substring(0, i);
                        break;
                    }
                }
                string fullPath = originalImage.File.DirectoryName + "\\" + name + originalImage.File.Extension;

                if (_thumbnailsDirectory != null)
                {
                    FileInfo thumbPath;
                    if (originalImage.File.FullName.StartsWith(_thumbnailsDirectory.FullName))
                        thumbPath = originalImage.File;
                    else
                        thumbPath = new FileInfo(Path.Combine(_thumbnailsDirectory.FullName, originalImage.RelativePath));
                    if (!thumbPath.Exists)
                    {
                        if (!thumbPath.Directory.Exists)
                            System.IO.Directory.CreateDirectory(thumbPath.Directory.FullName);
                        using (FileStream thumbFile = thumbPath.Create())
                        {
                            using (FileStream original = File.OpenRead(fullPath))
                            {
                                ImageWithMime thumb = PicturesEditor.GenerateThumbnail(original, _thumbnailsSize, true);
                                thumb.ImageStream.CopyTo(thumbFile);
                                thumb.ImageStream.Position = 0;
                                return thumb;
                            }
                        }
                    }
                    else
                    {
                        return new ImageWithMime(PicturesEditor.ConvertThumbnailExtension(thumbPath.Extension), thumbPath.OpenRead());
                    }
                }
                else
                {
                    using (FileStream original = File.OpenRead(fullPath))
                    {
                        return PicturesEditor.GenerateThumbnail(original, _thumbnailsSize, true);
                    }
                }
        }
Пример #3
0
        JsonResult IDriver.Init(string target)
        {
            FullPath fullPath;
            if (string.IsNullOrEmpty(target))
            {
                Root root = _roots.FirstOrDefault(r => r.StartPath != null);
                if (root == null)
                    root = _roots.First();
                fullPath = new FullPath(root, root.StartPath??root.Directory);
            }
            else
            {
                fullPath = ParsePath(target);
            }
            InitResponse answer = new InitResponse(DTOBase.Create(fullPath.Directory, fullPath.Root), new Options(fullPath));            

            foreach (FileInfo item in fullPath.Directory.GetFiles())
            {
                if ((item.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden)
                    answer.Files.Add(DTOBase.Create(item, fullPath.Root));
            }
            foreach (DirectoryInfo item in fullPath.Directory.GetDirectories())
            {
                if ((item.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden)
                    answer.Files.Add(DTOBase.Create(item, fullPath.Root));
            }
            foreach (Root item in _roots)
            {
                answer.Files.Add(DTOBase.Create(item.Directory, item));
            }
            if (fullPath.Root.Directory.FullName != fullPath.Directory.FullName)
            {
                foreach (DirectoryInfo item in fullPath.Root.Directory.GetDirectories())
                {
                    if ((item.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden)
                        answer.Files.Add(DTOBase.Create(item, fullPath.Root));
                }
            }
            if(fullPath.Root.MaxUploadSize.HasValue)
            {
                answer.UploadMaxSize = fullPath.Root.MaxUploadSizeInKb.Value + "K";
            }
            return Json(answer);
        }
Пример #4
0
        public FileSystemInfo GetFileByHash(string hash)
        {
            FullPath path = _driver.ParsePath(hash);

            return(path.Directory == null ? (FileSystemInfo)path.File : (FileSystemInfo)path.Directory);
        }
Пример #5
0
        JsonResult IDriver.Upload(string target, System.Web.HttpFileCollectionBase targets)
        {
            FullPath dest     = ParsePath(target);
            var      response = new AddResponse();

            if (dest.Root.MaxUploadSize.HasValue)
            {
                for (int i = 0; i < targets.AllKeys.Length; i++)
                {
                    HttpPostedFileBase file = targets[i];
                    if (file.ContentLength > dest.Root.MaxUploadSize.Value)
                    {
                        return(Error.MaxUploadFileSize());
                    }
                }
            }
            for (int i = 0; i < targets.AllKeys.Length; i++)
            {
                HttpPostedFileBase file = targets[i];
                string             path = Path.Combine(dest.Directory.FullName, file.FileName);

                if (File.Exists(path))
                {
                    if (dest.Root.UploadOverwrite)
                    {
                        //if file already exist we rename the current file,
                        //and if upload is succesfully delete temp file, in otherwise we restore old file
                        string tmpPath  = path + Guid.NewGuid();
                        bool   uploaded = false;
                        try
                        {
                            File.Move(path, tmpPath);
                            file.SaveAs(path);
                            uploaded = true;
                        }
                        catch { }
                        finally
                        {
                            if (uploaded)
                            {
                                File.Delete(tmpPath);
                            }
                            else
                            {
                                if (File.Exists(path))
                                {
                                    File.Delete(path);
                                }
                                File.Move(tmpPath, path);
                            }
                        }
                    }
                    else
                    {
                        string name = null;
                        for (int j = 1; j < 100; j++)
                        {
                            string suggestName = Path.GetFileNameWithoutExtension(file.FileName) + "-" + j + Path.GetExtension(file.FileName);
                            if (!File.Exists(Path.Combine(dest.Directory.FullName, suggestName)))
                            {
                                name = suggestName;
                                break;
                            }
                        }
                        if (name == null)
                        {
                            name = Path.GetFileNameWithoutExtension(file.FileName) + "-" + Guid.NewGuid() + Path.GetExtension(file.FileName);
                        }
                        path = Path.Combine(dest.Directory.FullName, name);
                        file.SaveAs(path);
                    }
                }
                else
                {
                    file.SaveAs(path);
                }
                response.Added.Add((FileDTO)DTOBase.Create(new FileInfo(path), dest.Root));
            }
            return(Json(response));
        }
Пример #6
0
        public JsonResult Upload(string target, HttpFileCollectionBase targets, bool addWithNewIndex)
        {
            FullPath dest     = ParsePath(target);
            var      response = new AddResponse();

            if (dest.Root.MaxUploadSize.HasValue)
            {
                for (int i = 0; i < targets.AllKeys.Length; i++)
                {
                    HttpPostedFileBase file = targets[i];
                    if (file.ContentLength > dest.Root.MaxUploadSize.Value)
                    {
                        return(Error.MaxUploadFileSize());
                    }
                }
            }
            for (int i = 0; i < targets.AllKeys.Length; i++)
            {
                HttpPostedFileBase file = targets[i];
                FileInfo           path = new FileInfo(Path.Combine(dest.Directory.FullName, Path.GetFileName(file.FileName)));

                if (path.Exists)
                {
                    if (dest.Root.UploadOverwrite)
                    {
                        //if file already exist we rename the current file,
                        //and if upload is succesfully delete temp file, in otherwise we restore old file
                        string tmpPath  = path.FullName + Guid.NewGuid();
                        bool   uploaded = false;
                        try
                        {
                            file.SaveAs(tmpPath);
                            uploaded = true;
                        }
                        catch { }
                        finally
                        {
                            if (uploaded)
                            {
                                File.Delete(path.FullName);
                                File.Move(tmpPath, path.FullName);
                            }
                            else
                            {
                                File.Delete(tmpPath);
                            }
                        }
                    }
                    else
                    {
                        file.SaveAs(Path.Combine(path.DirectoryName, Helper.GetDuplicatedName(path)));
                    }
                }
                else
                {
                    file.SaveAs(path.FullName);
                }
                response.Added.Add((FileDTO)DTOBase.Create(new FileInfo(path.FullName), dest.Root));
            }
            return(Json(response));
        }
Пример #7
0
        /// <summary> </summary>
        JsonResult IDriver.Init(string target, Hashtable posting_json_obj)
        {
            Root          root;
            DirectoryInfo dir;

            if (string.IsNullOrEmpty(target))
            {
                root = _roots.First();
                dir  = root.Directory;
            }
            else
            {
                FullPath fullPath = ParsePath(target);
                root = fullPath.Root;
                dir  = fullPath.Directory;
            }
            InitResponse answer = new InitResponse(DTOBase.Create(dir, root));


            foreach (var item in dir.GetFiles())
            {
                if (HttpContext.Current.Request.Params.AllKeys.Contains("mimes[]"))
                {
                    if (file_mime.mime_type(item.Extension.Trim('.')).IndexOf(HttpContext.Current.Request.Params["mimes[]"]) > -1)
                    {
                        answer.AddResponse(DTOBase.Create(item, root, posting_json_obj));
                    }
                }
                else
                {
                    answer.AddResponse(DTOBase.Create(item, root, posting_json_obj));
                }
            }
            foreach (var item in dir.GetDirectories())
            {
                answer.AddResponse(DTOBase.Create(item, root));
            }
            foreach (var item in _roots)
            {
                answer.AddResponse(DTOBase.Create(item.Directory, item));
            }
            foreach (var item in root.Directory.GetDirectories())
            {
                answer.AddResponse(DTOBase.Create(item, root));
            }
            //if (dir.FullName != root.Directory.FullName)
            //{
            //    foreach (var item in root.Directory.GetDirectories())
            //    {
            //        if (item.FullName == root.Directory.FullName)
            //        {
            //            foreach (var rootSubDir in item.GetDirectories())
            //            {
            //                if (rootSubDir.FullName != dir.FullName)
            //                {
            //                    answer.AddResponse(DTOBase.Create(rootSubDir, item));
            //                }
            //            }
            //        }
            //    }
            //}
            string parentPath = string.IsNullOrEmpty(target) ? root.Alias : root.Alias + dir.FullName.Substring(root.Directory.FullName.Length).Replace('\\', '/');

            answer.options.path   = parentPath;
            answer.options.url    = root.Url;
            answer.options.tmbUrl = root.TmbUrl;
            return(Json(answer));
        }