Ejemplo n.º 1
0
        public async Task <HttpResponseMessage> UploadDocument()
        {
            string authorizationString = DecodeAuthorizationString();

            SPHelper.SetSharePointCredentials(authorizationString);

            Guid fileId = Guid.Empty;

            if (!Request.Content.IsMimeMultipartContent())
            {
                HttpResponseException ex = new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
                throw ex;
            }

            SharePointDocument doc = new SharePointDocument();

            string targetFolderName = "";

            if (!string.IsNullOrEmpty(Request.RequestUri.Query))
            {
                List <KeyValuePair <string, string> > list = Request.GetQueryNameValuePairs().ToList <KeyValuePair <string, string> >();
                foreach (KeyValuePair <string, string> kvp in list)
                {
                    string key = kvp.Key;
                    string val = kvp.Value;
                    switch (key)
                    {
                    case "MasterId":
                        doc.MasterId = val;     //(val == "!" ? "" : val);
                        break;

                    case "DocumentType":
                        doc.DocumentType = StringToLookupItem("DocumentType", val);
                        break;

                    case "MasterNumber":
                        doc.MasterNumber = val;
                        break;

                    case "MasterName":
                        doc.MasterName = val;
                        break;

                    case "EntityName":
                        targetFolderName = SetEntityName(val);
                        break;

                    default:
                        break;
                    }
                }
            }

            if (!ValidateUploadFields(doc))
            {
                HttpResponseException ex = new HttpResponseException(HttpStatusCode.NotAcceptable);
                throw ex;
            }

            MultipartMemoryStreamProvider provider = new MultipartMemoryStreamProvider();
            await Request.Content.ReadAsMultipartAsync(provider);

            foreach (var file in provider.Contents)
            {
                var    fileName = file.Headers.ContentDisposition.FileName.Trim('\"');
                byte[] buffer   = await file.ReadAsByteArrayAsync();

                int maxItems = APISetting.GetInt("MAX_ITEMS_PER_FOLDER");
                if (maxItems == int.MinValue)
                {
                    maxItems = 5000;
                }

                if (!string.IsNullOrEmpty(doc.MasterId))
                {
                    ListItem msaFolder = SPHelper.FindFolder(doc.MasterId);
                    if (msaFolder == null)
                    {
                        ListItem rootFolder    = SPHelper.GetRootFolderForUpload(targetFolderName, true);
                        string   rootFolderUrl = rootFolder["FileRef"].ToString();
                        int      childFolders  = SPHelper.GetChildFoldersCount(rootFolderUrl);
                        if (childFolders < maxItems)
                        {
                            msaFolder        = SPHelper.CreateFolder(rootFolder, rootFolderUrl, doc.MasterId);
                            targetFolderName = doc.MasterId;
                        }
                        else
                        {
                            // CREATE NEXT PARENT FOLDER
                            int    currentIndex      = rootFolderUrl.Substring(rootFolderUrl.LastIndexOf('_') + 1).ToInt();
                            string newRootFolderName = APISetting.GetString("CUSTOMER_FOLDER_PREFIX") + (++currentIndex).ToString();
                            SPHelper.CreateRootFolder(newRootFolderName);
                            rootFolder       = SPHelper.GetRootFolderForUpload(targetFolderName, true);
                            rootFolderUrl    = rootFolder["FileRef"].ToString();
                            msaFolder        = SPHelper.CreateFolder(rootFolder, rootFolderUrl, doc.MasterId);
                            targetFolderName = doc.MasterId;
                        }
                    }
                    else
                    {
                        targetFolderName = doc.MasterId;
                    }
                }

                File uploadedFile = SPHelper.UploadFile(fileName, targetFolderName, buffer);
                Common.SPHelper.AddRequiredDocumentProperties(uploadedFile, doc.MasterId, doc.MasterNumber, doc.MasterName, doc.DocumentType);
                fileId = new Guid(uploadedFile.ListItemAllFields["UniqueId"].ToString());
            }

            HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, new KeyValuePair <string, string>("FileId", fileId.ToString()));

            return(response);
        }