JsonResult IDriver.Duplicate(IEnumerable<string> targets) { AddResponse response = new AddResponse(); foreach (var target in targets) { FullPath fullPath = ParsePath(target); if (fullPath.Directory != null) { var parentPath = fullPath.Directory.Parent.FullName; var name = fullPath.Directory.Name; var newName = string.Format(@"{0}\{1} copy", parentPath, name); if (!Directory.Exists(newName)) { DirectoryCopy(fullPath.Directory, newName, true); } else { for (int i = 1; i < 100; i++) { newName = string.Format(@"{0}\{1} copy {2}", parentPath, name, i); if (!Directory.Exists(newName)) { DirectoryCopy(fullPath.Directory, newName, true); break; } } } response.Added.Add(DTOBase.Create(new DirectoryInfo(newName), fullPath.Root)); } else { var parentPath = fullPath.File.Directory.FullName; var name = fullPath.File.Name.Substring(0, fullPath.File.Name.Length - fullPath.File.Extension.Length); var ext = fullPath.File.Extension; var newName = string.Format(@"{0}\{1} copy{2}", parentPath, name, ext); if (!File.Exists(newName)) { fullPath.File.CopyTo(newName); } else { for (int i = 1; i < 100; i++) { newName = string.Format(@"{0}\{1} copy {2}{3}", parentPath, name, i, ext); if (!File.Exists(newName)) { fullPath.File.CopyTo(newName); break; } } } response.Added.Add(DTOBase.Create(new FileInfo(newName), fullPath.Root)); } } return Json(response); }
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]; 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); }
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); }