/// <summary>
        /// Gets the issue attachment by id.
        /// </summary>
        /// <param name="attachmentId">The attachment id.</param>
        /// <returns></returns>
        public override IssueAttachment GetIssueAttachmentById(int attachmentId)
        {
            if (attachmentId <= 0) throw (new ArgumentOutOfRangeException("attachmentId"));

            IssueAttachment attachment = null;

            try
            {
                using (var sqlCmd = new SqlCommand())
                {
                    AddParamToSqlCmd(sqlCmd, "@IssueAttachmentId", SqlDbType.Int, 0, ParameterDirection.Input, attachmentId);
                    SetCommandType(sqlCmd, CommandType.StoredProcedure, SP_ISSUEATTACHMENT_GETATTACHMENTBYID);

                    // Execute Reader
                    if (_connectionString == string.Empty) throw (new ArgumentException("Connection string cannot be null or empty"));

                    using (var cn = new SqlConnection(_connectionString))
                    {
                        sqlCmd.Connection = cn;
                        cn.Open();
                        using (var rdr = sqlCmd.ExecuteReader())
                        {
                            if (rdr.Read())
                            {
                                byte[] attachmentData = null;

                                if (rdr["Attachment"] != DBNull.Value)
                                    attachmentData = (byte[])rdr["Attachment"];

                                attachment = new IssueAttachment
                                {
                                    Id = rdr.GetInt32(rdr.GetOrdinal("IssueAttachmentId")),
                                    Attachment = attachmentData,
                                    Description = rdr.GetString(rdr.GetOrdinal("Description")),
                                    DateCreated = rdr.GetDateTime(rdr.GetOrdinal("DateCreated")),
                                    ContentType = rdr.GetString(rdr.GetOrdinal("ContentType")),
                                    CreatorDisplayName = rdr.GetString(rdr.GetOrdinal("CreatorDisplayName")),
                                    CreatorUserName = rdr.GetString(rdr.GetOrdinal("CreatorUsername")),
                                    FileName = rdr.GetString(rdr.GetOrdinal("FileName")),
                                    IssueId = rdr.GetInt32(rdr.GetOrdinal("IssueId")),
                                    Size = rdr.GetInt32(rdr.GetOrdinal("FileSize"))
                                };
                            }
                        }
                    }   
                }

                return attachment;
            }
            catch (Exception ex)
            {
                throw ProcessException(ex);
            }
        }
        /// <summary>
        /// Saves the issue.
        /// </summary>
        /// <returns></returns>
        private bool SaveIssue()
        {
            decimal estimation;
            decimal.TryParse(txtEstimation.Text.Trim(), out estimation);
            var dueDate = DueDatePicker.SelectedValue ?? DateTime.MinValue;

            var issue = new Issue
                {
                    AffectedMilestoneId = DropAffectedMilestone.SelectedValue,
                    AffectedMilestoneImageUrl = string.Empty,
                    AffectedMilestoneName = DropAffectedMilestone.SelectedText,
                    AssignedDisplayName = DropAssignedTo.SelectedText,
                    AssignedUserId = Guid.Empty,
                    AssignedUserName = DropAssignedTo.SelectedValue,
                    CategoryId = DropCategory.SelectedValue,
                    CategoryName = DropCategory.SelectedText,
                    CreatorDisplayName = Security.GetDisplayName(),
                    CreatorUserId = Guid.Empty,
                    CreatorUserName = Security.GetUserName(),
                    DateCreated = DateTime.Now,
                    Description = DescriptionHtmlEditor.Text.Trim(),
                    Disabled = false,
                    DueDate = dueDate,
                    Estimation = estimation,
                    Id = 0,
                    IsClosed = false,
                    IssueTypeId = DropIssueType.SelectedValue,
                    IssueTypeName = DropIssueType.SelectedText,
                    IssueTypeImageUrl = string.Empty,
                    LastUpdate = DateTime.Now,
                    LastUpdateDisplayName = Security.GetDisplayName(),
                    LastUpdateUserName = Security.GetUserName(),
                    MilestoneDueDate = null,
                    MilestoneId = DropMilestone.SelectedValue,
                    MilestoneImageUrl = string.Empty,
                    MilestoneName = DropMilestone.SelectedText,
                    OwnerDisplayName = DropOwned.SelectedText,
                    OwnerUserId = Guid.Empty,
                    OwnerUserName = DropOwned.SelectedValue,
                    PriorityId = DropPriority.SelectedValue,
                    PriorityImageUrl = string.Empty,
                    PriorityName = DropPriority.SelectedText,
                    Progress = Convert.ToInt32(ProgressSlider.Text),
                    ProjectCode = string.Empty,
                    ProjectId = ProjectId,
                    ProjectName = string.Empty,
                    ResolutionId = DropResolution.SelectedValue,
                    ResolutionImageUrl = string.Empty,
                    ResolutionName = DropResolution.SelectedText,
                    StatusId = DropStatus.SelectedValue,
                    StatusImageUrl = string.Empty,
                    StatusName = DropStatus.SelectedText,
                    Title = Server.HtmlEncode(TitleTextBox.Text),
                    TimeLogged = 0,
                    Visibility = chkPrivate.Checked ? 1 : 0,
                    Votes = 0
                };

            if (!IssueManager.SaveOrUpdate(issue))
            {
                Message1.ShowErrorMessage(Resources.Exceptions.SaveIssueError);
                return false;
            }

            if (!CustomFieldManager.SaveCustomFieldValues(issue.Id, ctlCustomFields.Values, true))
            {
                Message1.ShowErrorMessage(Resources.Exceptions.SaveCustomFieldValuesError);
                return false;
            }

            IssueId = issue.Id;

            //add attachment if present.
            if (AspUploadFile.HasFile)
            {
                // get the current file
                var uploadFile = AspUploadFile.PostedFile;
                string inValidReason;
                var validFile = IssueAttachmentManager.IsValidFile(uploadFile.FileName, out inValidReason);

                if (validFile)
                {
                    if (uploadFile.ContentLength > 0)
                    {
                        byte[] fileBytes;
                        using (var input = uploadFile.InputStream)
                        {
                            fileBytes = new byte[uploadFile.ContentLength];
                            input.Read(fileBytes, 0, uploadFile.ContentLength);
                        }

                        var issueAttachment = new IssueAttachment
                        {
                            Id = Globals.NEW_ID,
                            Attachment = fileBytes,
                            Description = AttachmentDescription.Text.Trim(),
                            DateCreated = DateTime.Now,
                            ContentType = uploadFile.ContentType,
                            CreatorDisplayName = string.Empty,
                            CreatorUserName = Security.GetUserName(),
                            FileName = uploadFile.FileName,
                            IssueId = issue.Id,
                            Size = fileBytes.Length
                        };

                        if (!IssueAttachmentManager.SaveOrUpdate(issueAttachment))
                        {
                            Message1.ShowErrorMessage(string.Format(GetGlobalResourceObject("Exceptions", "SaveAttachmentError").ToString(), uploadFile.FileName));
                        }
                    }

                }
                else
                {
                    Message1.ShowErrorMessage(inValidReason);
                    return false;
                }
            }

            //create a vote for the new issue
            var vote = new IssueVote { IssueId = issue.Id, VoteUsername = Security.GetUserName() };

            if (!IssueVoteManager.SaveOrUpdate(vote))
            { 
                Message1.ShowErrorMessage(Resources.Exceptions.SaveIssueVoteError);
                return false;
            }

            if (chkNotifyOwner.Checked && !string.IsNullOrEmpty(issue.OwnerUserName))
            {
                var oUser = UserManager.GetUser(issue.OwnerUserName);
                if (oUser != null)
                {
                    var notify = new IssueNotification { IssueId = issue.Id, NotificationUsername = oUser.UserName };
                    IssueNotificationManager.SaveOrUpdate(notify);
                }
            }
            if (chkNotifyAssignedTo.Checked && !string.IsNullOrEmpty(issue.AssignedUserName))
            {
                var oUser = UserManager.GetUser(issue.AssignedUserName);
                if (oUser != null)
                {
                    var notify = new IssueNotification { IssueId = issue.Id, NotificationUsername = oUser.UserName };
                    IssueNotificationManager.SaveOrUpdate(notify);
                }
            }

            //send issue notifications
            IssueNotificationManager.SendIssueAddNotifications(issue.Id);

            return true;
        }
        /// <summary>
        /// Creates the new issue attachment.
        /// </summary>
        /// <param name="newAttachment">The new attachment.</param>
        /// <returns></returns>
        public override int CreateNewIssueAttachment(IssueAttachment newAttachment)
        {
            // Validate Parameters
            if (newAttachment == null) throw (new ArgumentNullException("newAttachment"));

            try
            {
                using (var sqlCmd = new SqlCommand())
                {
                    AddParamToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
                    AddParamToSqlCmd(sqlCmd, "@IssueId", SqlDbType.Int, 0, ParameterDirection.Input, newAttachment.IssueId);
                    AddParamToSqlCmd(sqlCmd, "@CreatorUserName", SqlDbType.NVarChar, 255, ParameterDirection.Input, newAttachment.CreatorUserName);
                    AddParamToSqlCmd(sqlCmd, "@FileName", SqlDbType.NVarChar, 250, ParameterDirection.Input, newAttachment.FileName);
                    AddParamToSqlCmd(sqlCmd, "@FileSize", SqlDbType.Int, 0, ParameterDirection.Input, newAttachment.Size);
                    AddParamToSqlCmd(sqlCmd, "@ContentType", SqlDbType.NVarChar, 80, ParameterDirection.Input, newAttachment.ContentType);
                    AddParamToSqlCmd(sqlCmd, "@Description", SqlDbType.NVarChar, 80, ParameterDirection.Input, newAttachment.Description);
                    if (newAttachment.Attachment != null)
                        AddParamToSqlCmd(sqlCmd, "@Attachment", SqlDbType.Image, newAttachment.Attachment.Length, ParameterDirection.Input, newAttachment.Attachment);
                    else
                        AddParamToSqlCmd(sqlCmd, "@Attachment", SqlDbType.Image, 0, ParameterDirection.Input, DBNull.Value);

                    SetCommandType(sqlCmd, CommandType.StoredProcedure, SP_ISSUEATTACHMENT_CREATE);
                    ExecuteScalarCmd(sqlCmd);
                    return ((int)sqlCmd.Parameters["@ReturnValue"].Value);   
                }
            }
            catch (Exception ex)
            {
                throw ProcessException(ex);
            }
        }
Beispiel #4
0
        /// <summary>
        /// Saves the mailbox entry.
        /// </summary>
        /// <param name="entry">The entry.</param>
        public void SaveMailboxEntry(MailboxEntry entry)
        {
            try
            {
                // TODO Should use XSLT templates BGN-1591
                string body = string.Format(this._BodyTemplate, entry.Content.ToString().Trim(), entry.From, entry.Date.ToString());
                int projectId = entry.ProjectMailbox.ProjectId;

                var mailIssue = IssueManager.GetDefaultIssueByProjectId(projectId, entry.Title.Trim(), body.Trim(), entry.ProjectMailbox.AssignToUserName, this._ReportingUserName);

                if (IssueManager.SaveOrUpdate(mailIssue))
                {
                    //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
                    foreach (Attachment attMail in entry.MailAttachments)
                    {
                        var attachmentBytes = new byte[attMail.ContentStream.Length];
                        ReadWholeArray(attMail.ContentStream, attachmentBytes);

                        var attachment = new IssueAttachment()
                                             {
                                                 Id = 0,
                                                 Attachment = attachmentBytes,
                                                 Description = "Attached via email",
                                                 DateCreated = DateTime.Now,
                                                 ContentType = attMail.ContentType.ToString(),
                                                 CreatorDisplayName = _ReportingUserName,
                                                 CreatorUserName = _ReportingUserName,
                                                 FileName = attMail.ContentDisposition.FileName,
                                                 IssueId = mailIssue.Id,
                                                 Size = attachmentBytes.Length
                                             };

                        if (!IssueAttachmentManager.SaveOrUpdate(attachment))
                            if (Log.IsWarnEnabled) Log.Warn("Attachment was not added via mailbox reader");
                    }
                }
            }
            catch (Exception ex)
            {
                throw ProcessException(ex);
            }
        }
Beispiel #5
0
 // IssueAttachments
 public abstract int CreateNewIssueAttachment(IssueAttachment newAttachment);
Beispiel #6
0
        private bool ProcessNewComment(List<string> recipients, POP3_ClientMessage message, Mail_Message mailHeader, MailboxReaderResult result)
        {
            string messageFrom = string.Empty;
            if (mailHeader.From.Count > 0)
            {
                messageFrom = string.Join("; ", mailHeader.From.ToList().Select(p => p.Address).ToArray()).Trim();
            }

            bool processed = false;

            foreach (var address in recipients)
            {
                Regex isReply = new Regex(@"(.*)(\+iid-)(\d+)@(.*)");
                Match commentMatch = isReply.Match(address);
                if (commentMatch.Success && commentMatch.Groups.Count >= 4)
                {
                    // we are in a reply and group 4 must contain the id of the original issue
                    int issueId;
                    if (int.TryParse(commentMatch.Groups[3].Value, out issueId))
                    {
                        var _currentIssue = IssueManager.GetById(issueId);

                        if (_currentIssue != null)
                        {
                            var project = ProjectManager.GetById(_currentIssue.ProjectId);

                            var mailbody = Mail_Message.ParseFromByte(message.MessageToByte());

                            bool isHtml;
                            List<MIME_Entity> attachments = null;
                            string content = GetMessageContent(mailbody, project, out isHtml, ref attachments);

                            IssueComment comment = new IssueComment
                            {
                                IssueId = issueId,
                                Comment = content,
                                DateCreated = mailHeader.Date
                            };

                            // try to find if the creator is valid user in the project, otherwise take
                            // the user defined in the mailbox config
                            var users = UserManager.GetUsersByProjectId(project.Id);
                            var emails = messageFrom.Split(';').Select(e => e.Trim().ToLower());
                            var user = users.Find(x => emails.Contains(x.Email.ToLower()));
                            if (user != null)
                            {
                                comment.CreatorUserName = user.UserName;
                            }
                            else
                            {
                                // user not found
                                continue;
                            }

                            var saved = IssueCommentManager.SaveOrUpdate(comment);
                            if (saved)
                            {
                                //add history record
                                var history = new IssueHistory
                                {
                                    IssueId = issueId,
                                    CreatedUserName = comment.CreatorUserName,
                                    DateChanged = comment.DateCreated,
                                    FieldChanged = ResourceStrings.GetGlobalResource(GlobalResources.SharedResources, "Comment", "Comment"),
                                    OldValue = string.Empty,
                                    NewValue = ResourceStrings.GetGlobalResource(GlobalResources.SharedResources, "Added", "Added"),
                                    TriggerLastUpdateChange = true
                                };
                                IssueHistoryManager.SaveOrUpdate(history);

                                var projectFolderPath = Path.Combine(Config.UploadsFolderPath, project.UploadPath);

                                // save attachments as new files
                                int attachmentsSavedCount = 1;
                                foreach (MIME_Entity mimeEntity in attachments)
                                {
                                    string fileName;
                                    var contentType = mimeEntity.ContentType.Type.ToLower();

                                    var attachment = new IssueAttachment
                                    {
                                        Id = 0,
                                        Description = "File attached by mailbox reader",
                                        DateCreated = DateTime.Now,
                                        ContentType = mimeEntity.ContentType.TypeWithSubtype,
                                        CreatorDisplayName = user.DisplayName,
                                        CreatorUserName = user.UserName,
                                        IssueId = issueId,
                                        ProjectFolderPath = projectFolderPath
                                    };
                                    attachment.Attachment = ((MIME_b_SinglepartBase)mimeEntity.Body).Data;

                                    if (contentType.Equals("attachment")) // this is an attached email
                                    {
                                        fileName = mimeEntity.ContentDisposition.Param_FileName;
                                    }
                                    else if (contentType.Equals("message")) // message has no filename so we create one
                                    {
                                        fileName = string.Format("Attached_Message_{0}.eml", attachmentsSavedCount);
                                    }
                                    else
                                    {
                                        fileName = string.IsNullOrWhiteSpace(mimeEntity.ContentType.Param_Name) ?
                                            string.Format("untitled.{0}", mimeEntity.ContentType.SubType) :
                                            mimeEntity.ContentType.Param_Name;
                                    }

                                    attachment.FileName = fileName;

                                    var saveFile = IsAllowedFileExtension(fileName);
                                    var fileSaved = false;

                                    // can we save the file?
                                    if (saveFile)
                                    {
                                        fileSaved = IssueAttachmentManager.SaveOrUpdate(attachment);

                                        if (fileSaved)
                                        {
                                            attachmentsSavedCount++;
                                        }
                                        else
                                        {
                                            LogWarning("MailboxReader: Attachment could not be saved, please see previous logs");
                                        }
                                    }
                                }

                                processed = true;

                                // add the entry if the save did not throw any exceptions
                                result.MailboxEntries.Add(new MailboxEntry());
                            }
                        }
                    }
                }
            }
            return processed;
        }
Beispiel #7
0
        /// <summary>
        /// Saves the mailbox entry.
        /// </summary>
        /// <param name="entry">The entry to be saved</param>
        Issue SaveMailboxEntry(MailboxEntry entry)
        {
            try
            {
                //load template 
                var body = string.Format("<div >Sent by:{1} on: {2}<br/>{0}</div>", entry.Content.Trim(), entry.From, entry.Date);

                if (Config.BodyTemplate.Trim().Length > 0)
                {
                    var data = new Dictionary<string, object> { { "MailboxEntry", entry } };
                    body = NotificationManager.GenerateNotificationContent(Config.BodyTemplate, data);
                }

                var projectId = entry.ProjectMailbox.ProjectId;

                // try to find if the creator is valid user in the project, otherwise take
                // the user defined in the mailbox config
                var creator = Config.ReportingUserName;
                var users = UserManager.GetUsersByProjectId(projectId);
                var emails = entry.From.Split(';').Select(e => e.Trim().ToLower());
                var user = users.Find(x => emails.Contains(x.Email.ToLower()));
                if (user != null)
                    creator = user.UserName;

                var mailIssue = IssueManager.GetDefaultIssueByProjectId(
                    projectId,
                    entry.Title.Trim(),
                    body.Trim(),
                    entry.ProjectMailbox.IssueTypeId,
                    entry.ProjectMailbox.AssignToUserName,
                    creator);

                if (entry.ProjectMailbox.CategoryId != 0)
                {
                    // overwrite default category with mailbox category
                    mailIssue.CategoryId = entry.ProjectMailbox.CategoryId;
                }

                if (!IssueManager.SaveOrUpdate(mailIssue)) return null;

                entry.IssueId = mailIssue.Id;
                entry.WasProcessed = true;

                var project = ProjectManager.GetById(projectId);

                var projectFolderPath = Path.Combine(Config.UploadsFolderPath, project.UploadPath);

                var doc = new HtmlDocument();
                doc.LoadHtml(mailIssue.Description); // load the issue body to we can process it for inline images (if exist)

                //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
                foreach (MIME_Entity mimeEntity in entry.MailAttachments)
                {
                    string fileName;
                    var isInline = false;
                    var contentType = mimeEntity.ContentType.Type.ToLower();

                    var attachment = new IssueAttachment
                        {
                            Id = 0,
                            Description = "File attached by mailbox reader",
                            DateCreated = DateTime.Now,
                            ContentType = mimeEntity.ContentType.TypeWithSubtype,
                            CreatorDisplayName = Config.ReportingUserName,
                            CreatorUserName = Config.ReportingUserName,
                            IssueId = mailIssue.Id,
                            ProjectFolderPath = projectFolderPath
                        };

                    switch (contentType)
                    {
                        case "application":
                            attachment.Attachment = ((MIME_b_SinglepartBase)mimeEntity.Body).Data;
                            break;
                        case "attachment":
                        case "image":
                        case "video":
                        case "audio":

                            attachment.Attachment = ((MIME_b_SinglepartBase)mimeEntity.Body).Data;
                            break;
                        case "message":

                            // we need to pull the actual email message out of the entity, and strip the "content type" out so that
                            // email programs will read the file properly
                            var messageBody = mimeEntity.ToString().Replace(mimeEntity.Header.ToString(), "");
                            if (messageBody.StartsWith("\r\n"))
                            {
                                messageBody = messageBody.Substring(2);
                            }

                            attachment.Attachment = Encoding.UTF8.GetBytes(messageBody);

                            break;
                        default:
                            LogWarning(string.Format("MailboxReader: Attachment type could not be processed {0}", mimeEntity.ContentType.Type));
                            break;
                    }

                    if (contentType.Equals("attachment")) // this is an attached email
                    {
                        fileName = mimeEntity.ContentDisposition.Param_FileName;
                    }
                    else if (contentType.Equals("message")) // message has no filename so we create one
                    {
                        fileName = string.Format("Attached_Message_{0}.eml", entry.AttachmentsSavedCount);
                    }
                    else
                    {
                        isInline = true;
                        fileName = string.IsNullOrWhiteSpace(mimeEntity.ContentType.Param_Name) ?
                            string.Format("untitled.{0}", mimeEntity.ContentType.SubType) :
                            mimeEntity.ContentType.Param_Name;
                    }

                    attachment.FileName = fileName;

                    var saveFile = IsAllowedFileExtension(fileName);
                    var fileSaved = false;

                    // can we save the file?
                    if (saveFile)
                    {
                        fileSaved = IssueAttachmentManager.SaveOrUpdate(attachment);

                        if (fileSaved)
                        {
                            entry.AttachmentsSavedCount++;
                        }
                        else
                        {
                            LogWarning("MailboxReader: Attachment could not be saved, please see previous logs");
                        }
                    }

                    if (!entry.IsHtml || !isInline) continue;

                    if (string.IsNullOrWhiteSpace(mimeEntity.ContentID)) continue;

                    var contentId = mimeEntity.ContentID.Replace("<", "").Replace(">", "").Replace("[", "").Replace("]", "");

                    // this is pretty greedy but since people might be sending screenshots I doubt they will send in dozens of images
                    // embedded in the email.  one would hope
                    foreach (var node in doc.DocumentNode.SelectNodes(XpathElementCaseInsensitive("img")).ToList())
                    {
                        var attr = node.Attributes.FirstOrDefault(p => p.Name.ToLowerInvariant() == "src");// get the src attribute

                        if (attr == null) continue; // image has no src attribute
                        if (!attr.Value.Contains(contentId)) continue; // is the attribute value the content id?

                        // swap out the content of the parent node html will our link to the image
                        var anchor = string.Format("<span class='inline-mail-attachment'>Inline Attachment: <a href='DownloadAttachment.axd?id={0}' target='_blank'>{1}</a></span>", attachment.Id, fileName);

                        // for each image in the body if the file was saved swap out the inline link for a link to the saved attachment
                        // otherwise blank out the content link so we don't get a missing image link
                        node.ParentNode.InnerHtml = fileSaved ? anchor : "";
                    }

                    mailIssue.Description = doc.DocumentNode.InnerHtml;
                    mailIssue.LastUpdateUserName = mailIssue.OwnerUserName;
                    mailIssue.LastUpdate = DateTime.Now;

                    IssueManager.SaveOrUpdate(mailIssue);
                }

                return mailIssue;
            }
            catch (Exception ex)
            {
                LogException(ex);
                throw;
            }
        }
Beispiel #8
0
        /// <summary>
        /// Uploads the document.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void UploadDocument(object sender, EventArgs e)
        {
            // get the current file
            var uploadFile = AspUploadFile.PostedFile;

            // if there was a file uploaded
            if (uploadFile.ContentLength > 0)
            {
                var inValidReason = string.Empty;
                var fileName = Path.GetFileName(uploadFile.FileName);

                var validFile = IssueAttachmentManager.IsValidFile(fileName, out inValidReason);

                if (validFile)
                {
                    byte[] fileBytes;
                    using (var input = uploadFile.InputStream)
                    {
                        fileBytes = new byte[uploadFile.ContentLength];
                        input.Read(fileBytes, 0, uploadFile.ContentLength);
                    }

                    var attachment = new IssueAttachment
                    {
                        Id = Globals.NEW_ID,
                        Attachment = fileBytes,
                        Description = AttachmentDescription.Text.Trim(),
                        DateCreated = DateTime.Now,
                        ContentType = uploadFile.ContentType,
                        CreatorDisplayName = string.Empty,
                        CreatorUserName = Security.GetUserName(),
                        FileName = fileName,
                        IssueId = IssueId,
                        Size = fileBytes.Length
                    };

                    if (!IssueAttachmentManager.SaveOrUpdate(attachment))
                    {
                        AttachmentsMessage.ShowErrorMessage(string.Format(GetGlobalResourceObject("Exceptions", "SaveAttachmentError").ToString(), uploadFile.FileName));
                        if (Log.IsWarnEnabled) Log.Warn(string.Format(GetGlobalResourceObject("Exceptions", "SaveAttachmentError").ToString(), uploadFile.FileName));
                        return;
                    }

                    //add history record and send notifications
                    var history = new IssueHistory
                                      {
                                          IssueId = IssueId,
                                          CreatedUserName = Security.GetUserName(),
                                          DateChanged = DateTime.Now,
                                          FieldChanged = ResourceStrings.GetGlobalResource(GlobalResources.SharedResources, "Attachment", "Attachment"),
                                          OldValue = fileName,
                                          NewValue = ResourceStrings.GetGlobalResource(GlobalResources.SharedResources, "Added", "Added")
                                      };

                    IssueHistoryManager.SaveOrUpdate(history);

                    var changes = new List<IssueHistory> {history};

                    IssueNotificationManager.SendIssueNotifications(IssueId, changes);

                    BindAttachments();
                }
                else
                    AttachmentsMessage.ShowErrorMessage(inValidReason);
            }
        }
Beispiel #9
0
        public bool CreateNewIssueAttachment(int issueId, string creatorUserName, string fileName, string contentType, byte[] attachment, int size, string description)
        {
            if (issueId <= 0) throw new ArgumentOutOfRangeException("issueId");

            var projectId = IssueManager.GetById(issueId).ProjectId;

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

            var issueAttachment = new IssueAttachment
            {
                Id = Globals.NEW_ID,
                Attachment = attachment,
                Description = description,
                DateCreated = DateTime.Now,
                ContentType = contentType,
                CreatorDisplayName = string.Empty,
                CreatorUserName = creatorUserName,
                FileName = fileName,
                IssueId = issueId,
                Size = size
            };

            return IssueAttachmentManager.SaveOrUpdate(issueAttachment);
        }
Beispiel #10
0
        /// <summary>
        /// Saves the issue.
        /// </summary>
        /// <returns></returns>
        private bool SaveIssue()
        {
            decimal estimation;
            decimal.TryParse(txtEstimation.Text.Trim(), out estimation);
            var dueDate = DueDatePicker.SelectedValue == null ? DateTime.MinValue : (DateTime)DueDatePicker.SelectedValue;

            var isNewIssue = (IssueId <= 0);

            // WARNING: DO NOT ENCODE THE HTMLEDITOR TEXT.
            // It expects raw input. So pass through a raw string.
            // This is a potential XSS vector as the Issue Class should
            // handle sanitizing the input and checking that its input is HtmlEncoded
            // (ie no < or > characters), not the IssueDetail.aspx.cs

            var issue = new Issue
                            {
                                AffectedMilestoneId = DropAffectedMilestone.SelectedValue,
                                AffectedMilestoneImageUrl = string.Empty,
                                AffectedMilestoneName = DropAffectedMilestone.SelectedText,
                                AssignedDisplayName = DropAssignedTo.SelectedText,
                                AssignedUserId = Guid.Empty,
                                AssignedUserName = DropAssignedTo.SelectedValue,
                                CategoryId = DropCategory.SelectedValue,
                                CategoryName = DropCategory.SelectedText,
                                CreatorDisplayName = Security.GetDisplayName(),
                                CreatorUserId = Guid.Empty,
                                CreatorUserName = Security.GetUserName(),
                                DateCreated = DateTime.Now,
                                Description = DescriptionHtmlEditor.Text.Trim(),
                                Disabled = false,
                                DueDate = dueDate,
                                Estimation = estimation,
                                Id = IssueId,
                                IsClosed = false,
                                IssueTypeId = DropIssueType.SelectedValue,
                                IssueTypeName = DropIssueType.SelectedText,
                                IssueTypeImageUrl = string.Empty,
                                LastUpdate = DateTime.Now,
                                LastUpdateDisplayName = Security.GetDisplayName(),
                                LastUpdateUserName = Security.GetUserName(),
                                MilestoneDueDate = null,
                                MilestoneId = DropMilestone.SelectedValue,
                                MilestoneImageUrl = string.Empty,
                                MilestoneName = DropMilestone.SelectedText,
                                OwnerDisplayName = DropOwned.SelectedText,
                                OwnerUserId = Guid.Empty,
                                OwnerUserName = DropOwned.SelectedValue,
                                PriorityId = DropPriority.SelectedValue,
                                PriorityImageUrl = string.Empty,
                                PriorityName = DropPriority.SelectedText,
                                Progress = Convert.ToInt32(ProgressSlider.Text),
                                ProjectCode = string.Empty,
                                ProjectId = ProjectId,
                                ProjectName = string.Empty,
                                ResolutionId = DropResolution.SelectedValue,
                                ResolutionImageUrl = string.Empty,
                                ResolutionName = DropResolution.SelectedText,
                                StatusId = DropStatus.SelectedValue,
                                StatusImageUrl = string.Empty,
                                StatusName = DropStatus.SelectedText,
                                Title = Server.HtmlEncode(TitleTextBox.Text),
                                TimeLogged = 0,
                                Visibility = chkPrivate.Checked ? 1 : 0,
                                Votes = 0
                            };

            if (!IssueManager.SaveOrUpdate(issue))
            {
                Message1.ShowErrorMessage(Resources.Exceptions.SaveIssueError);
                return false;
            }

            IssueId = issue.Id;

            if (!CustomFieldManager.SaveCustomFieldValues(IssueId, ctlCustomFields.Values))
            {
                Message1.ShowErrorMessage(Resources.Exceptions.SaveCustomFieldValuesError);
                return false;
            }

            //if new issue check if notify owner and assigned is checked.
            if (isNewIssue)
            {

                //add attachment if present.
                if (AspUploadFile.HasFile)
                {
                    // get the current file
                    var uploadFile = AspUploadFile.PostedFile;
                    string inValidReason;
                    var validFile = IssueAttachmentManager.IsValidFile(uploadFile.FileName, out inValidReason);

                    if (validFile)
                    {
                        if (uploadFile.ContentLength > 0)
                        {
                            byte[] fileBytes;
                            using (var input = uploadFile.InputStream)
                            {
                                fileBytes = new byte[uploadFile.ContentLength];
                                input.Read(fileBytes, 0, uploadFile.ContentLength);
                            }

                            var issueAttachment = new IssueAttachment
                            {
                                Id = Globals.NEW_ID,
                                Attachment = fileBytes,
                                Description = AttachmentDescription.Text.Trim(),
                                DateCreated = DateTime.Now,
                                ContentType = uploadFile.ContentType,
                                CreatorDisplayName = string.Empty,
                                CreatorUserName = Security.GetUserName(),
                                FileName = uploadFile.FileName,
                                IssueId = IssueId,
                                Size = fileBytes.Length
                            };

                            if (!IssueAttachmentManager.SaveOrUpdate(issueAttachment))
                            {
                                Message1.ShowErrorMessage(string.Format(GetGlobalResourceObject("Exceptions", "SaveAttachmentError").ToString(), uploadFile.FileName));
                            }
                        }

                    }
                    else
                    {
                        Message1.ShowErrorMessage(inValidReason);
                        return false;
                    }
                }

                //create a vote for the new issue
                var vote = new IssueVote { IssueId = IssueId, VoteUsername = Security.GetUserName() };

                if (!IssueVoteManager.SaveOrUpdate(vote))
                {
                    Message1.ShowErrorMessage(Resources.Exceptions.SaveIssueVoteError);
                    return false;
                }

                if (chkNotifyOwner.Checked && !string.IsNullOrEmpty(issue.OwnerUserName))
                {
                    var oUser = UserManager.GetUser(issue.OwnerUserName);
                    if (oUser != null)
                    {
                        var notify = new IssueNotification { IssueId = IssueId, NotificationUsername = oUser.UserName };
                        IssueNotificationManager.SaveOrUpdate(notify);
                    }
                }
                if (chkNotifyAssignedTo.Checked && !string.IsNullOrEmpty(issue.AssignedUserName))
                {
                    var oUser = UserManager.GetUser(issue.AssignedUserName);
                    if (oUser != null)
                    {
                        var notify = new IssueNotification { IssueId = IssueId, NotificationUsername = oUser.UserName };
                        IssueNotificationManager.SaveOrUpdate(notify);
                    }
                }

                // add all users subscribed at the project level to the issue level
                IEnumerable<ProjectNotification> subscriptions = ProjectNotificationManager.GetByProjectId(ProjectId);
                foreach (ProjectNotification sub in subscriptions)
                {
                    IssueNotificationManager.SaveOrUpdate(new IssueNotification()
                    {
                        IssueId = IssueId,
                        NotificationUsername = sub.NotificationUsername
                    });
                }

                //send issue notifications
                IssueNotificationManager.SendIssueAddNotifications(IssueId);
            }

            return true;
        }
        /// <summary>
        /// Saves this instance.
        /// </summary>
        /// <param name="entity">The issue attachment to save.</param>
        /// <returns></returns>
        public static bool SaveOrUpdate(IssueAttachment entity)
        {
            if (entity == null) throw new ArgumentNullException("entity");
            if (entity.IssueId <= Globals.NEW_ID) throw (new ArgumentException("Cannot save issue attachment, the issue id is invalid"));
            if (string.IsNullOrEmpty(entity.FileName)) throw (new ArgumentException("The attachment file name cannot be empty or null"));

            var invalidReason = string.Empty;

            if (!IsValidFile(entity.FileName, out invalidReason))
            {
                throw new ApplicationException(invalidReason);
            }

            //Start new save attachment code
            if (entity.Attachment.Length > 0)
            {
                // save the file to the upload directory
                var projectId = IssueManager.GetById(entity.IssueId).ProjectId;
                var p = ProjectManager.GetById(projectId);

                if (p.AllowAttachments)
                {
                    entity.ContentType = entity.ContentType.Replace("/x-png", "/png");
                    if (entity.ContentType == "image/bmp")
                    {
                        using (var ms = new MemoryStream(entity.Attachment, 0, entity.Attachment.Length))
                        {
                            ms.Write(entity.Attachment, 0, entity.Attachment.Length);
                            System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
                            img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                            ms.Seek(0, SeekOrigin.Begin);
                            entity.Attachment = ms.ToArray();

                        }
                        entity.ContentType = "image/png";
                        entity.FileName = Path.ChangeExtension(entity.FileName, "png");
                    }
                    entity.Size = entity.Attachment.Length;

                    if (p.AttachmentStorageType == IssueAttachmentStorageTypes.Database)
                    {
                        //save the attachment record to the database.
                        var tempId = DataProviderManager.Provider.CreateNewIssueAttachment(entity);
                        if (tempId > 0)
                        {
                            entity.Id = tempId;
                            return true;
                        }
                        return false;
                    }

                    var projectPath = p.UploadPath;

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

                        var attachmentGuid = Guid.NewGuid();
                        var attachmentBytes = entity.Attachment;
                        entity.Attachment = null;    //set attachment to null
                        entity.FileName = String.Format("{0}.{1}{2}", Path.GetFileNameWithoutExtension(entity.FileName), attachmentGuid, Path.GetExtension(entity.FileName));
                        var uploadedFilePath = HttpContext.Current.Server.MapPath("~" + Globals.UPLOAD_FOLDER + projectPath) + "\\" + entity.FileName;

                        //save the attachment record to the database.
                        var tempId = DataProviderManager.Provider.CreateNewIssueAttachment(entity);

                        if (tempId > 0)
                        {
                            entity.Id = tempId;
                            //save file to file system
                            var fi = new FileInfo(uploadedFilePath);

                            if(!Directory.Exists(fi.DirectoryName))
                                Directory.CreateDirectory(fi.DirectoryName);

                            var fs = File.Create(uploadedFilePath);
                            fs.Write(attachmentBytes, 0, entity.Size);
                            fs.Close();
                            return true;
                        }

                        return false;
                    }
                    catch (DirectoryNotFoundException ex)
                    {
                        if (Log.IsErrorEnabled)
                            Log.Error(string.Format(LoggingManager.GetErrorMessageResource("UploadPathNotFound"), projectPath), ex);
                        throw;
                    }
                    catch (Exception ex)
                    {
                        if (Log.IsErrorEnabled)
                            Log.Error(ex.Message, ex);
                        throw;
                    }
                }
            }

            return false;
        }
        /// <summary>
        /// Saves this instance.
        /// </summary>
        /// <param name="entity">The issue attachment to save.</param>
        /// <returns></returns>
        public static bool SaveOrUpdate(IssueAttachment entity)
        {
            if (entity == null) throw new ArgumentNullException("entity");
            if (entity.IssueId <= Globals.NEW_ID) throw (new ArgumentException("Cannot save issue attachment, the issue id is invalid"));
            if (String.IsNullOrEmpty(entity.FileName)) throw (new ArgumentException("The attachment file name cannot be empty or null"));

            var invalidReason = String.Empty;

            if (!IsValidFile(entity.FileName, out invalidReason))
            {
                throw new ApplicationException(invalidReason);
            }

            //Start new save attachment code
            if (entity.Attachment.Length > 0)
            {
                // save the file to the upload directory
                var projectId = IssueManager.GetById(entity.IssueId).ProjectId;
                var project = ProjectManager.GetById(projectId);

                if (project.AllowAttachments)
                {
                    entity.ContentType = entity.ContentType.Replace("/x-png", "/png");

                    if (entity.ContentType == "image/bmp")
                    {
                        using (var ms = new MemoryStream(entity.Attachment, 0, entity.Attachment.Length))
                        {
                            ms.Write(entity.Attachment, 0, entity.Attachment.Length);
                            var img = Image.FromStream(ms);
                            img.Save(ms, ImageFormat.Png);
                            ms.Seek(0, SeekOrigin.Begin);
                            entity.Attachment = ms.ToArray();

                        }

                        entity.ContentType = "image/png";
                        entity.FileName = Path.ChangeExtension(entity.FileName, "png");
                    }

                    entity.Size = entity.Attachment.Length;

                    if (HostSettingManager.Get(HostSettingNames.AttachmentStorageType, 0) == (int)IssueAttachmentStorageTypes.Database)
                    {
                        //save the attachment record to the database.
                        var tempId = DataProviderManager.Provider.CreateNewIssueAttachment(entity);
                        if (tempId > 0)
                        {
                            entity.Id = tempId;
                            return true;
                        }
                        return false;
                    }

                    var projectPath = project.UploadPath;

                    try
                    {
                        if (projectPath.Length == 0)
                            throw new ApplicationException(String.Format(LoggingManager.GetErrorMessageResource("UploadPathNotDefined"), project.Name));

                        var attachmentGuid = Guid.NewGuid();
                        var attachmentBytes = entity.Attachment;
                        entity.Attachment = null;    //set attachment to null    
                        entity.FileName = String.Format("{0}.{1}{2}", Path.GetFileNameWithoutExtension(entity.FileName), attachmentGuid, Path.GetExtension(entity.FileName));

                        var uploadedFilePath = string.Empty;

                        // added by WRH 2012-08-18
                        // this to fix the issue where attachments from the mailbox reader cannot be saved due to the lack of a http context.
                        // we need to supply the actual folder path on the entity
                        if (HttpContext.Current != null)
                        {
                            uploadedFilePath = string.Format(@"{0}\{1}", string.Format("{0}{1}", HostSettingManager.Get(HostSettingNames.AttachmentUploadPath), projectPath), entity.FileName);

                            if (uploadedFilePath.StartsWith("~"))
                            {
                                uploadedFilePath = HttpContext.Current.Server.MapPath(uploadedFilePath);
                            }
                        }
                        else
                        {
                            if(entity.ProjectFolderPath.Trim().Length > 0)
                            {
                                uploadedFilePath = string.Format("{0}\\{1}", entity.ProjectFolderPath, entity.FileName);
                            } 
                        }

                        //save the attachment record to the database.
                        var tempId = DataProviderManager.Provider.CreateNewIssueAttachment(entity);

                        if (tempId > 0)
                        {
                            entity.Id = tempId;

                            //save file to file system
                            var fi = new FileInfo(uploadedFilePath);

                            if(!Directory.Exists(fi.DirectoryName))
                                Directory.CreateDirectory(fi.DirectoryName);

                            File.WriteAllBytes(uploadedFilePath, attachmentBytes);

                            return true;
                        }

                        return false;
                    }
                    catch (DirectoryNotFoundException ex)
                    {
                        if (Log.IsErrorEnabled) 
                            Log.Error(String.Format(LoggingManager.GetErrorMessageResource("UploadPathNotFound"), projectPath), ex);
                        throw;
                    }
                    catch (Exception ex)
                    {
                        if (Log.IsErrorEnabled) 
                            Log.Error(ex.Message, ex);
                        throw;
                    }
                }
            }

            return false;
        }