Example #1
0
        public string AddPictureElement(int projectId, string token)
        {
            int userId = authenticationEngine.authenticate(token);
            Response.AddHeader("Access-Control-Allow-Origin", "*");
            if (Request.RequestType.Equals("OPTIONS", StringComparison.InvariantCultureIgnoreCase))  //This is a preflight request
            {
                Response.AddHeader("Access-Control-Allow-Methods", "POST, PUT");
                Response.AddHeader("Access-Control-Allow-Headers", "X-Requested-With");
                Response.AddHeader("Access-Control-Allow-Headers", "X-Request");
                Response.AddHeader("Access-Control-Max-Age", "86400"); //caching this policy for 1 day
                return null;
            }
            else
            {
                try
                {
                    //int newProjectElementId = -1;
                    JsonModels.UploadReponse response = new JsonModels.UploadReponse();
                    User user = userManager.GetUser(userId);

                    if (!projectManager.IsUserOwnerOfProject(projectId, user))
                    {
                        //return Json(new { Error = "Can't add picture at this time" });
                        return AddErrorHeader("You are not authorized to add this picture", 3);
                    }

                    if (Request != null)
                    {
                        if (Request.Files.Count == 0)
                        {
                            //return Json(new { Error = "No files submitted to server" });
                            return AddErrorHeader("No files submitted to server", 1);
                        }
                        else if (Request.Files[0].ContentLength == 0)
                        {
                            //return Json(new { Error = "No files submitted to server" });
                            return AddErrorHeader("No files submitted to server", 1);
                        }

                        foreach (string inputFileId in Request.Files)
                        {
                            HttpPostedFileBase file = Request.Files[inputFileId];
                            if (file.ContentLength > 0)
                            {
                                if (ValidationEngine.ValidatePicture(file) != ValidationEngine.Success)
                                {
                                    //return Json(new { Error = ValidationEngine.ValidatePicture(file) });
                                    return AddErrorHeader(ValidationEngine.ValidatePicture(file), 1);
                                }

                                System.IO.Stream fs = file.InputStream;
                                if (file.FileName.Contains(".jpeg") || file.FileName.Contains(".jpg") || file.FileName.Contains(".png") || file.FileName.Contains(".bmp") || file.FileName.Contains(".JPEG") || file.FileName.Contains(".JPG") || file.FileName.Contains(".PNG") || file.FileName.Contains(".BMP"))
                                {
                                    if (inputFileId == "newPictureUpload")
                                    {

                                        response = projectManager.UploadPictureElement(projectId, fs, file.FileName);
                                        if (response == null)
                                        {
                                            //return Json(new { Error = "An error occured saving the docuement." });
                                            return AddErrorHeader("An error occured saving the docuement.", 1);
                                        }
                                        aa.CreateAnalytic("Add Media", DateTime.Now, user.userName, file.FileName);
                                    }
                                }
                                else
                                {
                                    //return Json(new { Error = "File type not accepted" });
                                    return AddErrorHeader("File type not accepted", 1);
                                }
                            }
                        }
                    }
                    else
                    {
                        //return Json(new { Error = "Server did not receive file post" });
                        return AddErrorHeader("Server did not receive file post", 1);
                    }

                    //refresh the user object with the changes
                    user = userManager.GetUser(userId);
                    string returnVal;
                    try
                    {
                        returnVal = Serialize(response);
                    }
                    catch (Exception exception)
                    {
                        return AddErrorHeader(exception.Message, 1);
                    }
                    return AddSuccessHeader(returnVal);
                    //return Json(new { UpdatedPartial = RenderPartialViewToString("_Projects_Owner", new ProfileModel(user)), ProjectElementId = newProjectElementId });
                }
                catch (Exception ex)
                {
                    logAccessor.CreateLog(DateTime.Now, this.GetType().ToString() + "." + System.Reflection.MethodBase.GetCurrentMethod().Name.ToString(), ex.ToString());
                    //return Json(new { Error = "Problem saving media to cloud storage" });
                    return AddErrorHeader("Problem saving media to cloud storage", 1);
                }
            }
        }
Example #2
0
        public string AddDocumentElement(int projectId)
        {
            try
            {
                //int newProjectElementId = -1;
                JsonModels.UploadReponse response = new JsonModels.UploadReponse();
                User user = userManager.GetUser(User.Identity.Name);

                if (!projectManager.IsUserOwnerOfProject(projectId, user))
                {
                    //return Json(new { Error = "Can't add document at this time" });
                    return AddErrorHeader("Can't add document at this time", 1);
                }

                Project project;

                if (Request != null)
                {
                    if (Request.Files.Count == 0)
                    {
                        //return Json(new { Error = "No files submitted to server" });
                        return AddErrorHeader("No files submitted to server", 1);
                    }
                    else if (Request.Files[0].ContentLength == 0)
                    {
                        //return Json(new { Error = "No files submitted to server" });
                        return AddErrorHeader("No files submitted to server", 1);
                    }

                    foreach (string inputFileId in Request.Files)
                    {
                        HttpPostedFileBase file = Request.Files[inputFileId];
                        if (file.ContentLength > 0)
                        {
                            if (ValidationEngine.ValidateDocument(file) != ValidationEngine.Success)
                            {
                                //return Json(new { Error = ValidationEngine.ValidateDocument(file) });
                                return AddErrorHeader(ValidationEngine.ValidateDocument(file), 1);
                            }

                            System.IO.Stream fs = file.InputStream;

                            if (inputFileId == "newDocumentUpload")
                            {
                                response = projectManager.AddDocumentElement(projectId, null, fs, file.FileName, user.userName);

                                //check if this is development enviroment or LIVE
                                var account = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("BlobConnectionString"));

                                if (account.BlobEndpoint.IsLoopback)
                                {
                                    response.artifactURL = @"http://127.0.0.1:10000/devstoreaccount1/pdfs/" + response.artifactURL;
                                }
                                else
                                {
                                    response.artifactURL = RoleEnvironment.GetConfigurationSettingValue("storageAccountUrl").ToString()+"pdfs/" + response.artifactURL;//TODO change this when it goes live to vestnstorage
                                }

                                //--------------------------

                                if (response == null)
                                {
                                    return AddErrorHeader("File type not accepted", 1);
                                }
                                aa.CreateAnalytic("Add Media", DateTime.Now, user.userName, file.FileName);
                            }
                        }
                    }
                }
                else
                {
                    //return Json(new { Error = "Server did not receive file post" });
                    return AddErrorHeader("Server did not receive file post", 1);
                }

                //refresh the user object with the changes
                user = userManager.GetUser(User.Identity.Name);
                string returnVal;
                try
                {
                    returnVal = Serialize(response);
                }
                catch (Exception exception)
                {
                    return AddErrorHeader(exception.Message, 1);
                }
                return AddSuccessHeader(returnVal);
                //return Json(new { UpdatedPartial = RenderPartialViewToString("_Projects_Owner", new ProfileModel(user)), ProjectElementId = newProjectElementId });
            }
            catch (Exception ex)
            {
                logAccessor.CreateLog(DateTime.Now, this.GetType().ToString() + "." + System.Reflection.MethodBase.GetCurrentMethod().Name.ToString(), ex.ToString());
                //return Json(new { Error = "Problem saving media to cloud storage" });
                return AddErrorHeader("Problem saving media to cloud storage", 1);
            }
        }
Example #3
0
        public string AddAudioElement(int projectId)
        {
            try
            {
                JsonModels.UploadReponse response = new JsonModels.UploadReponse();
                User user = userManager.GetUser(User.Identity.Name);

                if (!projectManager.IsUserOwnerOfProject(projectId, user))
                {
                    return AddErrorHeader("Can't add audio at this time", 1);
                    //return Json(new { Error = "Can't add audio at this time" });
                }

                Project project;

                if (Request != null)
                {
                    if (Request.Files.Count == 0)
                    {
                        return AddErrorHeader("No files submitted to server", 1);
                        //return Json(new { Error = "No files submitted to server" });
                    }
                    else if (Request.Files[0].ContentLength == 0)
                    {
                        return AddErrorHeader("No files submitted to server", 1);
                        //return Json(new { Error = "No files submitted to server" });
                    }

                    foreach (string inputFileId in Request.Files)
                    {
                        HttpPostedFileBase file = Request.Files[inputFileId];
                        if (file.ContentLength > 0)
                        {
                            if (ValidationEngine.ValidateAudio(file) != ValidationEngine.Success)
                            {
                                return AddErrorHeader(ValidationEngine.ValidateAudio(file), 1);
                                //return Json(new { Error = ValidationEngine.ValidateAudio(file) });
                            }

                            System.IO.Stream fs = file.InputStream;

                            if (inputFileId == "newAudioUpload")
                            {
                                response = projectManager.AddAudioElement(projectId, null, fs, file.FileName);
                                if (response == null)
                                {
                                    return AddErrorHeader("Invalid Project Element ID", 1);
                                    //return Json(new { Error = "Invalid Project Element ID" });
                                }
                                aa.CreateAnalytic("Add Media", DateTime.Now, user.userName, file.FileName);
                            }
                        }
                    }
                }
                else
                {
                    return AddErrorHeader("Server did not receive file post", 1);
                    //return Json(new { Error = "Server did not receive file post" });
                }

                //refresh the user object with the changes
                user = userManager.GetUser(User.Identity.Name);
                string returnVal;
                try
                {
                    returnVal = Serialize(response);
                }
                catch (Exception exception)
                {
                    return AddErrorHeader(exception.Message, 1);
                }
                return AddSuccessHeader(returnVal);
                //return Json(new { UpdatedPartial = RenderPartialViewToString("_Projects_Owner", new ProfileModel(user)), ProjectElementId = newProjectElementId });
            }
            catch (Exception ex)
            {
                logAccessor.CreateLog(DateTime.Now, this.GetType().ToString() + "." + System.Reflection.MethodBase.GetCurrentMethod().Name.ToString(), ex.ToString());
                //return Json(new { Error = "Problem saving media to cloud storage" });
                return AddErrorHeader("Problem saving media to cloud storage", 1);
            }
        }
Example #4
0
        public string UpdateProject(int projectId = -1, string propertyId = null, string propertyValue = null, string token = "notset", string qqfile = null)
        {
            if (Request.RequestType.Equals("OPTIONS", StringComparison.InvariantCultureIgnoreCase))
            {
                return null;
            }
            try
            {
                int userId = -1;
                if (token != null)
                {
                    userId = authenticationEngine.authenticate(token);
                }
                else
                {
                    return AddErrorHeader("An authentication token must be passed in", 2);
                }
                if (userId < 0)
                {
                    return AddErrorHeader("You are not authenticated, please log in!", 2);
                }
                User user = userManager.GetUser(userId);
                Project project;
                if (projectId > 0)
                {
                    project = projectManager.GetProject(projectId);
                }
                else
                {
                    return AddErrorHeader("Invalid projectId", 1);
                }
                if (!projectManager.IsUserOwnerOfProject(projectId, user))
                {
                    return AddErrorHeader("User not authorized to update this project!", 3);
                }

                if (project == null)
                {
                    return AddErrorHeader("Project not found", 1);
                }

                System.Reflection.PropertyInfo pi = null;
                if (propertyId != null)
                {
                    pi = project.GetType().GetProperty(propertyId);
                }
                else
                {
                    AddErrorHeader("You must pass in a propertyId to set", 1);
                }

                if (pi == null)
                {
                    return AddErrorHeader("Invalid propertyId", 1);
                }
                else
                {
                    try
                    {
                        if (qqfile != null || Request.Files.Count != 0)
                        {
                            if (propertyId == "coverPicture")
                            {
                                var length = Request.ContentLength;
                                var bytes = new byte[length];
                                Request.InputStream.Read(bytes, 0, length);
                                Stream s = new MemoryStream(bytes);
                                JsonModels.UploadReponse response = new JsonModels.UploadReponse();
                                response = projectManager.UploadPictureElement(projectId, s, "coverPicture", true);
                                if (response == null)
                                {
                                    return AddErrorHeader("An error occured saving the docuement.", 1);
                                }
                                else
                                {
                                    return AddSuccessHeader(RoleEnvironment.GetConfigurationSettingValue("storageAccountUrl").ToString()+"thumbnails/" + response.artifactURL, true);
                                }
                            }
                        }
                        if (propertyValue != null)
                        {
                            //strip value of \n characters and replace with <br />
                            propertyValue = StripNewLineAndReplaceWithLineBreaks(propertyValue);
                        }
                        else
                        {
                            return AddErrorHeader("propertyValue not set", 1);
                        }
                        if (propertyId == "title")
                        {
                            if (ValidationEngine.ValidateTitle(propertyValue) != ValidationEngine.Success)
                            {
                                return AddErrorHeader("Title exceeded 100 character limit, project not updated", 1);
                            }
                        }
                        if (propertyId == "name")
                        {
                            if (ValidationEngine.ValidateTitle(propertyValue) != ValidationEngine.Success)
                            {
                                return AddErrorHeader("Name exceeded 100 character limit, project not updated", 1);
                            }
                        }
                        if (propertyId == "privacy")
                        {
                            //TODO - ensure what is added to the DB is of the Privacy enumeration
                        }
                        //TODO validate description
                        if (propertyId != "coverPicture")
                        {
                            pi.SetValue(project, Convert.ChangeType(propertyValue, pi.PropertyType), null);
                            //persist user model to DB with manager updateUser method
                            project = projectManager.UpdateProject(project);
                        }
                        if (project != null)
                        {
                            return AddSuccessHeader("Project with id:" + projectId + " successfully updated", true);
                        }
                        else
                        {
                            return AddErrorHeader("Update Failed", 1);
                        }
                    }
                    catch (Exception exc)
                    {

                        logAccessor.CreateLog(DateTime.Now, this.GetType().ToString() + "." + System.Reflection.MethodBase.GetCurrentMethod().Name.ToString(), exc.ToString());
                        return AddErrorHeader("Something went wrong while updating this project", 1);
                    }
                }
            }
            catch (Exception ex)
            {
                logAccessor.CreateLog(DateTime.Now, this.GetType().ToString() + "." + System.Reflection.MethodBase.GetCurrentMethod().Name.ToString(), ex.ToString());
                return AddErrorHeader("Something went wrong while updating this project", 1);
            }
        }
Example #5
0
 public string UpdateCoverPicture(int projectId, string token = null, string qqfile = null)
 {
     if (Request.RequestType.Equals("OPTIONS", StringComparison.InvariantCultureIgnoreCase))
     {
         return null;
     }
     try
     {
         int userId = -1;
         if (token != null)
         {
             userId = authenticationEngine.authenticate(token);
         }
         else
         {
             return AddErrorHeader("An authentication token must be passed in", 2);
         }
         if (userId < 0)
         {
             return AddErrorHeader("You are not authenticated, please log in!", 2);
         }
         User user = userManager.GetUser(userId);
         Project project;
         if (projectId > 0)
         {
             project = projectManager.GetProject(projectId);
         }
         else
         {
             return AddErrorHeader("Invalid projectId", 1);
         }
         if (project == null)
         {
             return AddErrorHeader("Project not found", 1);
         }
         if (!projectManager.IsUserOwnerOfProject(projectId, user))
         {
             return AddErrorHeader("User not authorized to update this project!", 3);
         }
         else
         {
             if (qqfile != null || Request.Files.Count == 1)
             {
                 var length = Request.ContentLength;
                 var bytes = new byte[length];
                 Stream s;
                 if (qqfile == "System.Web.HttpPostedFileWrapper")
                 {
                     qqfile = Request.Files[0].FileName;
                     s = Request.Files[0].InputStream;
                 }
                 else
                 {
                     Request.InputStream.Read(bytes, 0, length);
                     s = new MemoryStream(bytes);
                 }
                 JsonModels.UploadReponse response = new JsonModels.UploadReponse();
                 response = projectManager.UploadPictureElement(projectId, s, "coverPicture", true);
                 if (response == null)
                 {
                     return AddErrorHeader("An error occured saving the docuement.", 1);
                 }
                 else
                 {
                     activityManager.AddActivity(user.id, "Project Cover Picture", "Updated", projectId);
                     return AddSuccessHeader(RoleEnvironment.GetConfigurationSettingValue("storageAccountUrl").ToString()+"thumbnails/" + response.artifactURL, true);
                 }
             }
             else
             {
                 return AddErrorHeader("No files were posted to the server", 1);
             }
         }
     }
     catch (Exception ex)
     {
         logAccessor.CreateLog(DateTime.Now, "ProjectController - UpdateCoverPicture", ex.StackTrace);
         return AddErrorHeader("Something went wrond while updating this project's cover picture", 1);
     }
 }
Example #6
0
        public string AddArtifact_Media(int projectId, string token, string qqfile=null)
        {
            if (Request.RequestType.Equals("OPTIONS", StringComparison.InvariantCultureIgnoreCase))  //This is a preflight request
            {
                return null;
            }
            else
            {
                try
                {
                    int userId = authenticationEngine.authenticate(token);
                    if (userId < 0)
                    {
                        return AddErrorHeader("You are not authenticated, please log in!", 2);
                    }
                    //int newProjectElementId = -1;
                    JsonModels.UploadReponse response = new JsonModels.UploadReponse();
                    User user = userManager.GetUser(userId);
                    string artifactType = "null";
                    if (!projectManager.IsUserOwnerOfProject(projectId, user))
                    {
                        //return Json(new { Error = "Can't add picture at this time" });
                        return AddErrorHeader("You are not authorized to add this picture", 3);
                    }
                    if (Request != null)
                    {
                        if (qqfile == null && Request.Files.Count == 0)
                        {
                            return AddErrorHeader("No files submitted to server", 1);
                        }

                        var length = Request.ContentLength;
                        var bytes = new byte[length];
                        Stream s;
                        if (qqfile == "System.Web.HttpPostedFileWrapper")
                        {
                            qqfile = Request.Files[0].FileName;
                            s = Request.Files[0].InputStream;
                        }
                        else
                        {
                            Request.InputStream.Read(bytes, 0, length);
                            s = new MemoryStream(bytes);
                        }
                        //logAccessor.CreateLog(DateTime.Now, "Upload Artifact Media - 700", "request.files[0].filename:"+Request.Files[0].FileName);
                        //logAccessor.CreateLog(DateTime.Now, "Upload Artifact Media - 701", "request.files[0].contenttype:" + Request.Files[0].ContentType);

                        if (qqfile.Contains(".jpeg") || qqfile.Contains(".jpg") || qqfile.Contains(".png") || qqfile.Contains(".bmp") || qqfile.Contains(".JPEG") || qqfile.Contains(".JPG") || qqfile.Contains(".PNG") || qqfile.Contains(".BMP"))
                        {
                            response = projectManager.UploadPictureElement(projectId, s, qqfile);
                            if (response == null)
                            {
                                return AddErrorHeader("An error occured saving the docuement.", 1);
                            }
                            aa.CreateAnalytic("Add_Media", DateTime.Now, user.userName, "Picture");
                            artifactType = "picture";
                        }
                        else if (qqfile.Contains(".PDF") || qqfile.Contains(".pdf") || qqfile.Contains(".doc") || qqfile.Contains(".docx") || qqfile.Contains(".ppt") || qqfile.Contains(".pptx") || qqfile.Contains(".xls") || qqfile.Contains(".xlsx") || qqfile.Contains(".txt") || qqfile.Contains(".rtf") || qqfile.Contains(".DOC") || qqfile.Contains(".DOCX") || qqfile.Contains(".PPT") || qqfile.Contains(".PPTX") || qqfile.Contains(".XLS") || qqfile.Contains(".XLSX") || qqfile.Contains(".TXT") || qqfile.Contains(".RTF"))
                        {
                            response = projectManager.AddDocumentElement(projectId, null, s, qqfile, user.userName);

                            //check if this is development enviroment or LIVE
                            var account = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("BlobConnectionString"));

                            if (response == null)
                            {
                                return AddErrorHeader("File type not accepted", 1);
                            }
                            aa.CreateAnalytic("Add_Media", DateTime.Now, user.userName, "Document");
                            artifactType = "document";
                        }
                        else
                        {
                            return AddErrorHeader("You did not upload an accepted picture or document type: (jpeg, jpg, png, bmp, doc, docx, ppt, pptx, xls, xlsx, txt, rtf", 1);
                        }
                    }
                    else
                    {
                        return AddErrorHeader("Server did not receive file post", 1);
                    }
                    //refresh the user object with the changes
                    user = userManager.GetUser(userId);
                    //build the artifact response

                    JsonModels.Artifact artifactResponse = new JsonModels.Artifact();
                    artifactResponse.id = response.id;
                    if(artifactType == "picture")
                    {
                        artifactResponse.artifactLocation = RoleEnvironment.GetConfigurationSettingValue("storageAccountUrl").ToString()+"thumbnails/" + response.artifactURL;
                        artifactResponse.fileLocation = response.fileURL;
                    }
                    else if (artifactType == "document")
                    {
                        artifactResponse.artifactLocation = response.artifactURL;
                        artifactResponse.fileLocation = response.fileURL;
                    }

                    artifactResponse.title = response.name;
                    artifactResponse.type = artifactType;
                    artifactResponse.creationDate = DateTime.Now.ToString();

                    string realReturnVal;
                    try
                    {
                        realReturnVal = Serialize(artifactResponse);
                    }
                    catch (Exception exception)
                    {
                        return AddErrorHeader(exception.Message, 1);
                    }
                    activityManager.AddActivity(user.id, "Artifact", "Added", artifactResponse.id);
                    return AddSuccessHeader(realReturnVal);
                }
                catch (Exception ex)
                {
                    logAccessor.CreateLog(DateTime.Now, this.GetType().ToString() + "." + System.Reflection.MethodBase.GetCurrentMethod().Name.ToString(), ex.ToString());
                    return AddErrorHeader("Problem saving media to cloud storage", 1);
                }
            }
        }