private void WriteCheckIfExists(HttpContext context) { FS = Engine.Resolve <IFileSystem>(); var parentDirectory = context.Request["selected"]; var filenames = Selection.RequestValueAccessor("filenames"); var selected = new Directory(DirectoryData.Virtual(parentDirectory), null); if (string.IsNullOrEmpty(parentDirectory) || !Engine.SecurityManager.IsAuthorized(context.User, selected, N2.Security.Permission.Read)) { context.Response.WriteJson(new { Status = "Error", Message = "Not allowed" }); return; } if (string.IsNullOrEmpty(filenames)) { context.Response.WriteJson(new { Status = "Error", Message = "No files selected" }); return; } var fns = filenames.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries); context.Response.WriteJson( new { Status = "Checked", Files = (from fileName in fns let newPath = Url.Combine(context.Request.ApplicationPath + selected.Url, HttpUtility.UrlDecode(fileName)) where FS.FileExists(newPath) select fileName).ToArray() }); }
private void WriteUploadFile(HttpContext context) { ValidateTicket(context.Request["ticket"]); FS = Engine.Resolve <IFileSystem>(); var parentDirectory = context.Request["selected"]; var selected = new Directory(DirectoryData.Virtual(parentDirectory), null); if (string.IsNullOrEmpty(parentDirectory) || !Engine.SecurityManager.IsAuthorized(context.User, selected, N2.Security.Permission.Write)) { context.Response.WriteJson(new { Status = "Error", Message = "Not allowed" }); return; } var overwriteStr = Selection.RequestValueAccessor("overwrite"); var overwrite = overwriteStr.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries); var baseDir = context.Request.ApplicationPath + selected.Url; try { if (context.Request.Files.Count == 0) { context.Response.WriteJson(new { Status = "Ok", Message = "No files uploaded" }); return; } foreach (string file in context.Request.Files) { var fileContent = context.Request.Files[file]; if (fileContent == null || fileContent.ContentLength <= 0) { continue; } var stream = fileContent.InputStream; var fileName = fileContent.FileName; if (string.IsNullOrEmpty(fileName)) { continue; } var newPath = Url.Combine(baseDir, fileName); if (overwrite != null && overwrite.Any(p => p == fileName)) { FS.WriteFile(newPath, stream); } // Overwrite = yes else { var incr = 1; var extension = VirtualPathUtility.GetExtension(fileName); var extPos = fileName.LastIndexOf(extension); var origFilenameWithoutExtension = fileName.Substring(0, extPos); var restOfFilename = fileName.Substring(extPos); while (FS.FileExists(newPath)) { fileName = string.Format("{0}-{1}{2}", origFilenameWithoutExtension, incr, restOfFilename); newPath = Url.Combine(baseDir, fileName); incr++; } FS.WriteFile(newPath, stream); } // Overwrite = no } context.Response.WriteJson(new { Status = "Ok", Message = "Files uploaded" }); return; } catch (Exception e) { context.Response.WriteJson(new { Status = "Error", Message = "Upload failed", Detail = e.Message }); return; } }
private void WriteUploadFile(HttpContext context) { ValidateTicket(context.Request["ticket"]); FS = Engine.Resolve <IFileSystem>(); var parentDirectory = context.Request["selected"]; var selected = new Directory(DirectoryData.Virtual(parentDirectory), null); if (string.IsNullOrEmpty(parentDirectory) || !Engine.SecurityManager.IsAuthorized(context.User, selected, N2.Security.Permission.Write)) { context.Response.WriteJson(new { Status = "Error", Message = "Not allowed" }); return; } var overwriteStr = Selection.RequestValueAccessor("overwrite"); var overwrite = overwriteStr.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries); try { if (context.Request.Files.Count == 0) { context.Response.WriteJson(new { Status = "Ok", Message = "No files uploaded" }); return; } foreach (string file in context.Request.Files) { var fileContent = context.Request.Files[file]; if (fileContent == null || fileContent.ContentLength <= 0) { continue; } var stream = fileContent.InputStream; var fileName = System.IO.Path.GetFileName(fileContent.FileName); if (string.IsNullOrEmpty(fileName)) { continue; } var newPath = System.IO.Path.Combine(context.Server.MapPath(parentDirectory), fileName); if (overwrite != null && overwrite.Any(p => p == fileName)) { var vPath = selected.Url + fileName; FS.WriteFile(vPath, stream); } // Overwrite = yes else { var incr = 1; while (System.IO.File.Exists(newPath)) { fileName = string.Format("{0}-{1}{2}", System.IO.Path.GetFileNameWithoutExtension(fileName), incr, System.IO.Path.GetExtension(fileName)); newPath = System.IO.Path.Combine(context.Server.MapPath(parentDirectory), fileName); } var vPath = selected.Url + fileName; FS.WriteFile(vPath, stream); } // Overwrite = no } context.Response.WriteJson(new { Status = "Ok", Message = "Files uploaded" }); return; } catch (Exception) { context.Response.WriteJson(new { Status = "Error", Message = "Upload failed" }); return; } }
private void WriteSearch(HttpContext context) { var query = context.Request["query"]; if (string.IsNullOrWhiteSpace(query)) { context.Response.WriteJson(new { Status = "Error", Total = 0, Message = "Please provide a search term." }); return; } query = query.TrimStart().TrimEnd(); var host = Engine.Resolve <IHost>(); var rootItem = Engine.Persister.Get(host.DefaultSite.RootItemID); var root = new HierarchyNode <ContentItem>(rootItem); FS = Engine.Resolve <IFileSystem>(); var selectionTrail = Find.EnumerateParents(Selection.SelectedItem, null, true).ToList().Where(a => a is AbstractNode).Reverse().ToList(); var uploadDirectories = MediaBrowserUtils.GetAvailableUploadFoldersForAllSites(context, root, selectionTrail, Engine, FS); if (uploadDirectories.Count == 0) { context.Response.WriteJson(new { Status = "Error", Total = 0, Message = "No available directories in this site." }); return; } //Do the search using the IFileSystem var resultsFileData = FS.SearchFiles(query, uploadDirectories); if (!resultsFileData.Any()) { context.Response.WriteJson(new { Status = "Error", Total = 0, Message = "0 files found." }); return; } var files = new List <File>(); var fileMap = new Dictionary <string, File>(StringComparer.OrdinalIgnoreCase); var lastParent = string.Empty; AbstractDirectory parent = null; foreach (var fd in resultsFileData.OrderBy(s => s.VirtualPath)) { var parentDirectory = fd.VirtualPath.Substring(0, fd.VirtualPath.LastIndexOf('/')); if (lastParent != parentDirectory) { parent = new Directory(DirectoryData.Virtual(parentDirectory), null); lastParent = parentDirectory; } var file = new File(fd, parent); file.Set(FS); file.Set(ImageSizes); var unresizedFileName = ImageSizes.RemoveImageSize(file.Name); if (unresizedFileName != null && fileMap.ContainsKey(unresizedFileName)) { fileMap[unresizedFileName].Add(file); if (ImageSizes.GetSizeName(file.Name) == "icon") { file.IsIcon = true; } } else { if (unresizedFileName == null) { files.Add(file); fileMap[file.Name] = file; } } } files.Sort(new TitleComparer <File>()); var directory = FS.GetDirectory("/"); var fsRootPath = directory != null && !string.IsNullOrWhiteSpace(directory.RootPath) ? directory.RootPath : ""; var selectableExtensions = context.Request["exts"]; context.Response.WriteJson(new { Path = "", Total = files.Count, Files = GetFileReducedList(files, ImageSizes, selectableExtensions, fsRootPath) }); }
private void WriteSearch(HttpContext context) { var query = context.Request["query"]; if (string.IsNullOrWhiteSpace(query)) { context.Response.WriteJson(new { Total = 0, Message = "Please provide a search term" }); return; } query = query.TrimStart().TrimEnd(); var host = Engine.Resolve <IHost>(); var rootItem = Engine.Persister.Get(host.DefaultSite.RootItemID); var root = new HierarchyNode <ContentItem>(rootItem); FS = Engine.Resolve <IFileSystem>(); var selectionTrail = Find.EnumerateParents(Selection.SelectedItem, null, true).ToList().Where(a => a is AbstractNode).Reverse().ToList(); var uploadDirectories = MediaBrowserUtils.GetAvailableUploadFoldersForAllSites(context, root, selectionTrail, Engine, FS); if (uploadDirectories.Count == 0) { context.Response.WriteJson(new { Total = 0, Message = "No available directories" }); return; } if (query.IndexOf('*') < 0) { query = "*" + query + "*"; } var resultFilenames = new List <string>(); foreach (var dir in uploadDirectories) { //Search, returns Absolute Paths resultFilenames.AddRange( System.IO.Directory.GetFiles( System.Web.Hosting.HostingEnvironment.MapPath("~" + dir.Current.Url), query, System.IO.SearchOption.AllDirectories ) ); } if (resultFilenames.Count == 0) { context.Response.WriteJson(new { Total = 0, Message = "0 files found" }); return; } List <File> files = new List <File>(); var fileMap = new Dictionary <string, File>(StringComparer.OrdinalIgnoreCase); string lastParent = null; AbstractDirectory parent = null; foreach (var nm in resultFilenames.OrderBy(s => s)) { var fd = FS.GetFile(AbsolutePathToVirtual(nm)); var parentDirectory = fd.VirtualPath.Substring(0, fd.VirtualPath.LastIndexOf('/')); if (lastParent != parentDirectory) { parent = new Directory(DirectoryData.Virtual(parentDirectory), null); lastParent = parentDirectory; } var file = new File(fd, parent); file.Set(FS); file.Set(ImageSizes); var unresizedFileName = ImageSizes.RemoveImageSize(file.Name); if (unresizedFileName != null && fileMap.ContainsKey(unresizedFileName)) { fileMap[unresizedFileName].Add(file); if (ImageSizes.GetSizeName(file.Name) == "icon") { file.IsIcon = true; } } else { if (unresizedFileName == null) { files.Add(file); fileMap[file.Name] = file; } } } files.Sort(new TitleComparer <File>()); var selectableExtensions = context.Request["exts"]; context.Response.WriteJson(new { Path = "", Total = files.Count, Files = GetFileReducedList(files, ImageSizes, selectableExtensions) }); }