/// <summary>
        /// Uploads the file.
        /// </summary>
        /// <param name="issueId">The issue id.</param>
        /// <param name="uploadFile">The upload file.</param>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        public static void UploadFile(int issueId, HttpPostedFile uploadFile, HttpContext context, string description)
        {
            if (issueId <= 0)
                    throw new ArgumentOutOfRangeException("issueId");
                if (uploadFile == null)
                    throw new ArgumentNullException("uploadFile");
                if (uploadFile.ContentLength == 0)
                    throw new ArgumentOutOfRangeException("uploadFile");
                if (context == null)
                    throw new ArgumentNullException("context");

                // get the current file
                //HttpPostedFile uploadFile = this.AspUploadFile.PostedFile;
                //HttpContext context = HttpContext.Current;

                // if there was a file uploaded
                if (uploadFile.ContentLength > 0)
                {
                    bool isFileOk = false;
                    string[] AllowedFileTypes = HostSetting.GetHostSetting("AllowedFileExtensions").Split(';');
                    string fileExt = System.IO.Path.GetExtension(uploadFile.FileName);
                    string uploadedFileName = string.Empty;

                    uploadedFileName = Path.GetFileName(uploadFile.FileName);

                    if (AllowedFileTypes.Length > 0 && AllowedFileTypes[0].CompareTo("*.*") == 0)
                    {
                        isFileOk = true;
                    }
                    else
                    {

                        foreach (string fileType in AllowedFileTypes)
                        {
                            string newfileType = fileType.Substring(fileType.LastIndexOf("."));
                            if (newfileType.CompareTo(fileExt) == 0)
                            {
                                isFileOk = true;
                                break;
                            }

                        }
                    }

                    //file type is not valid
                    if (!isFileOk)
                    {
                        if (Log.IsErrorEnabled) Log.Error(string.Format(Logging.GetErrorMessageResource("InvalidFileType"), uploadedFileName));
                        return;
                    }

                    //check for illegal filename characters
                    if (uploadedFileName.IndexOfAny(System.IO.Path.GetInvalidFileNameChars()) != -1)
                    {
                        if (Log.IsErrorEnabled) Log.Error(string.Format(Logging.GetErrorMessageResource("InvalidFileName"), uploadedFileName));
                        return;
                    }

                    //if the file is ok save it.
                    if (isFileOk)
                    {
                        // save the file to the upload directory
                        int projectId = Issue.GetIssueById(issueId).ProjectId;
                        Project p = Project.GetProjectById(projectId);

                        if (p.AllowAttachments)
                        {
                            IssueAttachment attachment;

                            Stream input = uploadFile.InputStream;
                            int fileSize = uploadFile.ContentLength;
                            string ctype = uploadFile.ContentType.Replace("/x-png", "/png");
                            if (uploadFile.ContentType == "image/bmp")
                            {
                                MemoryStream memstr = new MemoryStream();
                                System.Drawing.Image img = System.Drawing.Image.FromStream(uploadFile.InputStream);
                                img.Save(memstr, System.Drawing.Imaging.ImageFormat.Png);
                                memstr.Seek(0, SeekOrigin.Begin);
                                input = memstr;
                                fileSize = (int)memstr.Length;
                                ctype = "image/png";
                                uploadedFileName = Path.ChangeExtension(uploadedFileName, "png");
                            }
                            byte[] fileBytes = new byte[fileSize];
                            input.Read(fileBytes, 0, fileSize);
                            if (p.AttachmentStorageType == IssueAttachmentStorageType.Database)
                            {

                                attachment = new IssueAttachment(
                                    issueId,
                                    HttpContext.Current.User.Identity.Name,
                                    uploadedFileName,
                                    ctype,
                                    fileBytes,
                                    fileSize,
                                    description);

                                attachment.Save();
                            }
                            else
                            {
                                string ProjectPath = p.UploadPath;

                                try
                                {
                                    if (ProjectPath.Length == 0)
                                        throw new ApplicationException(string.Format(Logging.GetErrorMessageResource("UploadPathNotDefined"), p.Name));

                                    string UploadedFileName = String.Format("{0:0000}_", issueId) + System.IO.Path.GetFileName(uploadedFileName);
                                    string UploadedFilePath = context.Server.MapPath("~" + Globals.UploadFolder + ProjectPath) + "\\" + UploadedFileName;
                                    attachment = new IssueAttachment(issueId, context.User.Identity.Name, UploadedFileName, ctype, null, fileSize, description);
                                    if (attachment.Save())
                                    {
                                        FileStream fs = File.Create(UploadedFilePath);
                                        fs.Write(fileBytes, 0, fileSize);
                                        fs.Close();
                                        return;
                                    }
                                }
                                catch (DirectoryNotFoundException ex)
                                {
                                    if (Log.IsErrorEnabled) Log.Error(string.Format(Logging.GetErrorMessageResource("UploadPathNotFound"), ProjectPath), ex);
                                    throw;
                                }
                                catch (Exception ex)
                                {
                                    if (Log.IsErrorEnabled) Log.Error(ex.Message, ex);
                                    throw;
                                }
                            }

                        }

                    }
                }
        }
Beispiel #2
0
        /// <summary>
        /// Saves the entry.
        /// </summary>
        /// <param name="entry">The entry.</param>
        public void SaveEntry(Entry entry)
        {
            try
            {
                string body = string.Format(this.BodyTemplate, entry.Content.ToString().Trim(), entry.From, entry.Date.ToString());
                int curPID = entry.ProjectMailbox.ProjectId;

                Issue MailIssue = Issue.GetDefaultIssueByProjectId(curPID, entry.Title.Trim(), body.Trim(), entry.ProjectMailbox.AssignToName, ReportingUserName);

                if (MailIssue.Save())
                {
                    //If there is an attached file present then add it to the database
                    //and copy it to the directory specified in the web.config file
                    for (int i = 0; i < entry.AttachmentFileNames.Count; i++)
                    {
                        MailAttachment attMail = entry.MailAttachments[i] as MailAttachment;
                        IssueAttachment att = new IssueAttachment(0,
                            MailIssue.Id,
                            ReportingUserName,
                            ReportingUserName,
                            DateTime.Now,
                            Path.GetFileName(entry.AttachmentFileNames[i].ToString()),
                            attMail.ContentType.ToString(),
                            attMail.Data,
                            attMail.Data.Length, "Attached via email"
                            );
                        att.Save();
                    }
                }
            }
            catch (Exception ex)
            {
                throw ProcessException(ex);
            }
        }
        public bool CreateNewIssueAttachment(int issueId, string creatorUserName, string fileName, string contentType, byte[] attachment, int size, string description)
        {
            if (issueId <= 0)
                throw new ArgumentOutOfRangeException("issueId");

            int projectId = Issue.GetIssueById(issueId).ProjectId;

            //authentication checks against user access to project
            if (Project.GetProjectById(projectId).AccessType == Globals.ProjectAccessType.Private && !Project.IsUserProjectMember(UserName, projectId))
                throw new UnauthorizedAccessException(string.Format(Logging.GetErrorMessageResource("ProjectAccessDenied"), UserName));

            IssueAttachment issueAttachment = new IssueAttachment(issueId, creatorUserName, fileName, contentType, attachment, size, description);
            return issueAttachment.Save();
        }