Esempio n. 1
0
        /// <summary>
        /// Gets the project image by id.
        /// </summary>
        /// <param name="projectId">The project id.</param>
        /// <returns></returns>
        public override ProjectImage GetProjectImageById(int projectId)
        {
            if (projectId <= 0) throw (new ArgumentOutOfRangeException("projectId"));

            try
            {
                using (var sqlCmd = new SqlCommand())
                {
                    AddParamToSqlCmd(sqlCmd, "@ProjectId", SqlDbType.Int, 0, ParameterDirection.Input, projectId);
                    sqlCmd.CommandText = "SELECT [ProjectId], [ProjectImageFileContent],[ProjectImageFileName],[ProjectImageContentType],[ProjectImageFileSize] FROM BugNet_Projects WHERE ProjectId = @ProjectId";

                    ProjectImage projectImage = null;

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

                                if (dtr["ProjectImageFileContent"] != DBNull.Value)
                                    attachmentData = (byte[])dtr["ProjectImageFileContent"];
                                else
                                    return null;

                                projectImage = new ProjectImage((int)dtr["ProjectId"], attachmentData, (string)dtr["ProjectImageFileName"], (long)dtr["ProjectImageFileSize"], (string)dtr["ProjectImageContentType"]);

                            }   
                        }
                    }

                    return projectImage;   
                }
            }
            catch (Exception ex)
            {
                throw ProcessException(ex);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Updates this instance.
        /// </summary>
        /// <returns></returns>
        public bool Update()
        {
            if (Page.IsValid)
            {
                var at = (rblAccessType.SelectedValue == "Public") ? Globals.ProjectAccessType.Public : Globals.ProjectAccessType.Private;
                var attachmentStorageType = (AttachmentStorageType.SelectedValue == "2") ? IssueAttachmentStorageTypes.Database : IssueAttachmentStorageTypes.FileSystem;

                ProjectImage projectImage = null;

                // get the current file
                var uploadFile = ProjectImageUploadFile.PostedFile;

                // if there was a file uploaded
                if (uploadFile.ContentLength > 0)
                {
                    var isFileOk = false;
                    var allowedFileTypes = new string[3] { ".gif", ".png", ".jpg" };
                    var fileExt = Path.GetExtension(uploadFile.FileName);
                    var uploadedFileName = Path.GetFileName(uploadFile.FileName);

                    foreach (var newfileType in allowedFileTypes.Select(fileType => fileType.Substring(fileType.LastIndexOf("."))).Where(newfileType => newfileType.CompareTo(fileExt) == 0))
                    {
                        isFileOk = true;
                    }

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

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

                    var fileSize = uploadFile.ContentLength;
                    var fileBytes = new byte[fileSize];
                    var myStream = uploadFile.InputStream;
                    myStream.Read(fileBytes, 0, fileSize);

                    projectImage = new ProjectImage(ProjectId, fileBytes, uploadedFileName, fileSize, uploadFile.ContentType);
                }

                var project = new Project
                                      {
                                          AccessType = at,
                                          Name = txtName.Text.Trim(),
                                          Id = ProjectId,
                                          CreatorUserName = Page.User.Identity.Name,
                                          CreatorDisplayName = string.Empty,
                                          Description = ProjectDescriptionHtmlEditor.Text.Trim(),
                                          AllowAttachments = AllowAttachments.Checked,
                                          AllowIssueVoting = chkAllowIssueVoting.Checked,
                                          AttachmentStorageType = attachmentStorageType,
                                          Code = ProjectCode.Text.Trim(),
                                          Disabled = false,
                                          Image = projectImage,
                                          ManagerDisplayName = string.Empty,
                                          ManagerUserName = ProjectManager.SelectedValue,
                                          SvnRepositoryUrl = string.Empty,
                                          UploadPath = txtUploadPath.Text.Trim()
                                      };

                if (BLL.ProjectManager.SaveOrUpdate(project))
                {
                    ProjectId = project.Id;
                    return true;
                }

                lblError.Text = LoggingManager.GetErrorMessageResource("SaveProjectError");
            }

            return false;
        }