public ActionResult AddDocument(DocumentViewModel model)
        {
            AuthUser authUser = Authenticator.AuthenticatedUser;

            HttpPostedFileBase file   = Request.Files["file"];
            string             fName  = string.Empty;
            string             fURL   = string.Empty;
            string             folder = string.Empty;

            if (file != null && file.ContentLength > 0)
            {
                fName = file.FileName.Replace(" ", "_");
                //folder = "user-" + authUser.Uuid.Substring(24) + "/documents/";
                Tuple <string, string> tupleUrl = storageService.UploadPublicFile(file.InputStream, fName, containerBucketName);
                fURL = tupleUrl.Item1;

                Document newDoc = new Document()
                {
                    Name         = fName,
                    CreationDate = DateUtil.GetDateTimeNow(),
                    Type         = model.DocumentType,
                    URL          = tupleUrl.Item1,
                    Uuid         = tupleUrl.Item2,
                    User         = new User()
                    {
                        Id = authUser.Id
                    }
                };
                _documentService.Create(newDoc);
            }

            return(Json(new { Message = fName, URL = fURL, Success = true }));
        }
示例#2
0
        public HttpResponseMessage AddDocuments([FromBody] DocumentRequest request)
        {
            try
            {
                List <MessageResponse> messages = new List <MessageResponse>();
                int UserId = GetUserId();

                foreach (DocumentObject documentObj in request.Documents)
                {
                    bool   validContent = false;
                    string fileName     = documentObj.Name;
                    if (!string.IsNullOrWhiteSpace(documentObj.Base64))
                    {
                        validContent = true;
                        var                    data     = Convert.FromBase64String(documentObj.Base64);
                        MemoryStream           ms       = new MemoryStream(data);
                        Tuple <string, string> tupleUrl = storageService.UploadPublicFile(ms, fileName, containerBucketName);
                        Document               newDoc   = new Document()
                        {
                            Name         = fileName,
                            CreationDate = DateUtil.GetDateTimeNow(),
                            Type         = documentObj.Type,
                            URL          = tupleUrl.Item1,
                            Uuid         = tupleUrl.Item2,
                            User         = new User()
                            {
                                Id = UserId
                            }
                        };
                        _documentService.Create(newDoc);
                        messages.Add(new MessageResponse
                        {
                            Type        = MessageType.info.ToString("G"),
                            Description = string.Format("Documento agregado correctamente: {0} UUID: {1} URL: {2}", newDoc.Name, newDoc.Uuid, newDoc.URL),
                        });
                    }
                    if (!string.IsNullOrWhiteSpace(documentObj.URL))
                    {
                        validContent = true;
                        Document newDoc = new Document()
                        {
                            Name         = fileName,
                            CreationDate = DateUtil.GetDateTimeNow(),
                            Type         = documentObj.Type,
                            URL          = documentObj.URL,
                            Uuid         = Guid.NewGuid().ToString(),
                            User         = new User()
                            {
                                Id = UserId
                            }
                        };
                        _documentService.Create(newDoc);
                        messages.Add(new MessageResponse
                        {
                            Type        = MessageType.info.ToString("G"),
                            Description = string.Format("Documento agregado correctamente: {0} UUID: {1} URL: {2}", newDoc.Name, newDoc.Uuid, newDoc.URL),
                        });
                    }
                    if (!validContent)
                    {
                        messages.Add(new MessageResponse {
                            Type = MessageType.error.ToString("G"), Description = string.Format("No se encontró el contenido del documento: {0}", documentObj.Name)
                        });
                    }
                }

                return(CreateResponse(messages));
            }
            catch (Exception e)
            {
                return(CreateErrorResponse(e));
            }
        }