示例#1
0
        void grdAttachments_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
        {
            string sGuid = e.CommandArgument.ToString();
            if (sGuid.Length != 36) { return; }
            Guid attachmentGuid = new Guid(sGuid);

            FileAttachment fileAttachment;

            switch (e.CommandName)
            {
                case "delete":

                    fileAttachment = new FileAttachment(attachmentGuid);
                    SiteUtils.DeleteAttachmentFile(fileSystem, fileAttachment, upLoadPath);
                    FileAttachment.Delete(attachmentGuid);

                    WebUtils.SetupRedirect(this, Request.RawUrl);

                    break;

                //case "download":

                //    //these files are not protected extensions so a simple link to download is better

                //    fileAttachment = new FileAttachment(attachmentGuid);

                //    string downloadPath = upLoadPath + fileAttachment.ServerFileName;

                //    string fileType = System.IO.Path.GetExtension(fileAttachment.FileName).Replace(".", string.Empty);

                //    if (string.Equals(fileType, "pdf", StringComparison.InvariantCultureIgnoreCase))
                //    {
                //        //this will display the pdf right in the browser
                //        Page.Response.AddHeader("Content-Disposition", "filename=" + fileAttachment.FileName);
                //    }
                //    else
                //    {
                //        // other files just use file save dialog
                //        Page.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileAttachment.FileName);
                //    }

                //    Page.Response.ContentType = "application/" + fileType;
                //    Page.Response.Buffer = false;
                //    Page.Response.BufferOutput = false;
                //    //Page.Response.TransmitFile(downloadPath);
                //    //Page.Response.End();
                //    using (System.IO.Stream stream = fileSystem.GetAsStream(downloadPath))
                //    {
                //        stream.CopyTo(Page.Response.OutputStream);
                //    }
                //    try
                //    {
                //        Page.Response.End();
                //    }
                //    catch (System.Threading.ThreadAbortException) { }

                //    break;

            }
        }
示例#2
0
        public void ProcessRequest(HttpContext context)
        {
            base.Initialize(context);

            if (!UserCanEditModule(ModuleId, Blog.FeatureGuid))
            {
                log.Info("User has no edit permission so returning 404");
                Response.StatusCode = 404;
                return;
            }

            if (CurrentSite == null)
            {
                log.Info("CurrentSite is null so returning 404");
                Response.StatusCode = 404;
                return;
            }

            if (CurrentUser == null)
            {
                log.Info("CurrentUser is null so returning 404");
                Response.StatusCode = 404;
                return;
            }

            if (FileSystem == null)
            {
                log.Info("FileSystem is null so returning 404");
                Response.StatusCode = 404;
                return;
            }

            if (Request.Files.Count == 0)
            {
                log.Info("Posted File Count is zero so returning 404");
                Response.StatusCode = 404;
                return;
            }

            if (Request.Files.Count > BlogConfiguration.MaxAttachmentsToUploadAtOnce)
            {
                log.Info("Posted File Count is higher than allowed so returning 404");
                Response.StatusCode = 404;
                return;
            }

            itemId = WebUtils.ParseInt32FromQueryString("ItemID", itemId);

            if (itemId == -1)
            {
                log.Info("No ItemID provided so returning 404");
                Response.StatusCode = 404;
                return;
            }

            module = GetModule(ModuleId, Blog.FeatureGuid);

            if (module == null)
            {
                log.Info("Module is null so returning 404");
                Response.StatusCode = 404;
                return;
            }

            blog = new Blog(itemId);
            if (blog.ModuleId != ModuleId)
            {
                log.Info("Invalid ItemID for module so returning 404");
                Response.StatusCode = 404;
                return;
            }

            Hashtable moduleSettings = ModuleSettings.GetModuleSettings(ModuleId);
            config = new BlogConfiguration(moduleSettings);

            context.Response.ContentType = "text/plain";//"application/json";
            var r = new System.Collections.Generic.List<UploadFilesResult>();
            JavaScriptSerializer js = new JavaScriptSerializer();

            SiteUtils.EnsureFileAttachmentFolder(CurrentSite);
            string upLoadPath = SiteUtils.GetFileAttachmentUploadPath();

            for (int f = 0; f < Request.Files.Count; f++)
            {
                HttpPostedFile file = Request.Files[f];

                string ext = System.IO.Path.GetExtension(file.FileName);

                if (!SiteUtils.IsAllowedUploadBrowseFile(ext, WebConfigSettings.AllowedMediaFileExtensions))
                {
                    log.Info("file extension was " + ext + " so discarding file " + file.FileName);

                    r.Add(new UploadFilesResult()
                    {
                        Name = file.FileName,
                        Length = file.ContentLength,
                        Type = file.ContentType,
                        ErrorMessage = string.Format(
                            CultureInfo.InvariantCulture,
                            GalleryResources.InvalidUploadExtensionFormat,
                            file.FileName,
                            WebConfigSettings.AllowedMediaFileExtensions.Replace("|", " "))

                    });

                    continue;
                }

                string mimeType = IOHelper.GetMimeType(ext).ToLower();

                FileAttachment a = new FileAttachment();
                a.CreatedBy = CurrentUser.UserGuid;
                a.FileName = System.IO.Path.GetFileName(file.FileName);
                a.ServerFileName = blog.ItemId.ToInvariantString() + a.FileName.ToCleanFileName(WebConfigSettings.ForceLowerCaseForUploadedFiles);
                a.ModuleGuid = blog.ModuleGuid;
                a.SiteGuid = CurrentSite.SiteGuid;
                a.ItemGuid = blog.BlogGuid;
                a.ContentLength = file.ContentLength;
                a.ContentType = mimeType;

                a.Save();

                string destPath = upLoadPath + a.ServerFileName;

                using (Stream s = file.InputStream)
                {
                    FileSystem.SaveFile(destPath, s, mimeType, true);
                }

                r.Add(new UploadFilesResult()
                {
                    //Thumbnail_url =
                    Name = a.FileName,
                    Length = file.ContentLength,
                    Type = mimeType
                });

                if (WebConfigSettings.LogAllFileServiceRequests)
                {
                    string userName = "******";
                    if (CurrentUser != null)
                    {
                        userName = CurrentUser.Name;
                    }
                    log.Info("File " + file.FileName + " uploaded by " + userName + " as a media attachment in the Blog");

                }

            }

            var uploadedFiles = new
            {
                files = r.ToArray()
            };

            var jsonObj = js.Serialize(uploadedFiles);
            context.Response.Write(jsonObj.ToString());
        }
示例#3
0
 /// <summary>
 /// Compares 2 instances of FileAttachment.
 /// </summary>
 public static int CompareByCreatedUtc(FileAttachment fileAttachment1, FileAttachment fileAttachment2)
 {
     return(fileAttachment1.CreatedUtc.CompareTo(fileAttachment2.CreatedUtc));
 }
示例#4
0
        protected void btnUpload_Click(object sender, EventArgs e)
        {
            if (blog == null) { return; }
            if (currentUser == null) { return; }
            if (fileSystem == null) { return; }

            if (uploader.HasFile)
            {
                string ext = System.IO.Path.GetExtension(uploader.FileName);
                string mimeType = IOHelper.GetMimeType(ext).ToLower();
                if (SiteUtils.IsAllowedUploadBrowseFile(ext, WebConfigSettings.AllowedMediaFileExtensions))
                {
                    FileAttachment f = new FileAttachment();
                    f.CreatedBy = currentUser.UserGuid;
                    f.FileName = System.IO.Path.GetFileName(uploader.FileName);
                    f.ServerFileName = blog.ItemId.ToInvariantString() + f.FileName.ToCleanFileName(WebConfigSettings.ForceLowerCaseForUploadedFiles);
                    f.ModuleGuid = blog.ModuleGuid;
                    f.SiteGuid = siteSettings.SiteGuid;
                    f.ItemGuid = blog.BlogGuid;
                    f.ContentLength = uploader.FileBytes.Length;
                    f.ContentType = mimeType;

                    f.Save();
                    string destPath = upLoadPath + f.ServerFileName;
                    //fileInput.MoveTo(destPath, MoveToOptions.Overwrite);

                    using (uploader.FileContent)
                    {
                        fileSystem.SaveFile(destPath, uploader.FileContent, mimeType, true);
                    }

                }
            }

            WebUtils.SetupRedirect(this, Request.RawUrl);
        }
示例#5
0
 /// <summary>
 /// Compares 2 instances of FileAttachment.
 /// </summary>
 public static int CompareByFileName(FileAttachment fileAttachment1, FileAttachment fileAttachment2)
 {
     return(fileAttachment1.FileName.CompareTo(fileAttachment2.FileName));
 }
示例#6
0
        //public static void DeleteAttachmentFiles(List<FileAttachment> attachments, string basePath)
        //{
        //    if (attachments == null) { return; }
        //    if(string.IsNullOrEmpty(basePath)) { return;}
        //    foreach (FileAttachment f in attachments)
        //    {
        //        if(File.Exists(Path.Combine(basePath, f.ServerFileName)))
        //        {
        //            File.Delete(Path.Combine(basePath, f.ServerFileName));
        //        }
        //    }
        //}
        //public static void DeleteAttachmentFile(FileAttachment attachment, string basePath)
        //{
        //    if (attachment == null) { return; }
        //    if(string.IsNullOrEmpty(basePath)) { return;}
        //    if(File.Exists(Path.Combine(basePath, attachment.ServerFileName)))
        //    {
        //        File.Delete(Path.Combine(basePath, attachment.ServerFileName));
        //    }
        //}
        private static List<FileAttachment> LoadListFromReader(IDataReader reader)
        {
            List<FileAttachment> fileAttachmentList = new List<FileAttachment>();
            try
            {
                while (reader.Read())
                {
                    FileAttachment fileAttachment = new FileAttachment();
                    fileAttachment.rowGuid = new Guid(reader["RowGuid"].ToString());
                    fileAttachment.siteGuid = new Guid(reader["SiteGuid"].ToString());
                    fileAttachment.moduleGuid = new Guid(reader["ModuleGuid"].ToString());
                    fileAttachment.itemGuid = new Guid(reader["ItemGuid"].ToString());
                    fileAttachment.specialGuid1 = new Guid(reader["SpecialGuid1"].ToString());
                    fileAttachment.specialGuid2 = new Guid(reader["SpecialGuid2"].ToString());
                    fileAttachment.serverFileName = reader["ServerFileName"].ToString();
                    fileAttachment.fileName = reader["FileName"].ToString();
                    fileAttachment.createdUtc = Convert.ToDateTime(reader["CreatedUtc"]);
                    fileAttachment.createdBy = new Guid(reader["CreatedBy"].ToString());

                    if (reader["ContentLength"] != DBNull.Value)
                    {
                        fileAttachment.contentLength = Convert.ToInt64(reader["ContentLength"]);
                    }

                    fileAttachment.contentTitle = reader["ContentTitle"].ToString();
                    fileAttachment.contentType = reader["ContentType"].ToString();

                    fileAttachmentList.Add(fileAttachment);

                }
            }
            finally
            {
                reader.Close();
            }

            return fileAttachmentList;
        }
示例#7
0
 /// <summary>
 /// Compares 2 instances of FileAttachment.
 /// </summary>
 public static int CompareByServerFileName(FileAttachment fileAttachment1, FileAttachment fileAttachment2)
 {
     return fileAttachment1.ServerFileName.CompareTo(fileAttachment2.ServerFileName);
 }
示例#8
0
 /// <summary>
 /// Compares 2 instances of FileAttachment.
 /// </summary>
 public static int CompareByCreatedUtc(FileAttachment fileAttachment1, FileAttachment fileAttachment2)
 {
     return fileAttachment1.CreatedUtc.CompareTo(fileAttachment2.CreatedUtc);
 }
示例#9
0
        public static void DeleteAttachmentFile(IFileSystem fileSystem, FileAttachment attachment, string basePath)
        {
            if (attachment == null) { return; }
            if (string.IsNullOrEmpty(basePath)) { return; }

            if (fileSystem.FileExists(basePath + attachment.ServerFileName))
            {
                fileSystem.DeleteFile(basePath + attachment.ServerFileName);
            }
        }