Exemplo n.º 1
0
        public static int Delete(Models.DocumentLibrary library)
        {
            using (var db = new Context.SqlContext())
            {
                db.Entry(library).State = library.DocumentLibraryId.Equals(0) ? EntityState.Unchanged : EntityState.Deleted;
                var ret = db.SaveChanges();

                return(ret);
            }
        }
Exemplo n.º 2
0
        public static long Save(Models.DocumentLibrary library)
        {
            using (var db = new Context.SqlContext())
            {
                db.Entry(library).State = library.DocumentLibraryId.Equals(0) ? EntityState.Added : EntityState.Modified;
                var ret = db.SaveChanges();

                return(library.DocumentLibraryId);
            }
        }
        public async Task <ActionResult> SyncDocumentLibrary(string documentlibraryid)
        {
            try
            {
                if (documentlibraryid != null)
                {
                    Models.DocumentLibrary docLib = Repository.DocumentLibrary.Get(Convert.ToInt64(documentlibraryid));

                    // delete files from Azure
                    AzureBlobStorage azureStorage = new AzureBlobStorage();
                    string           error        = await azureStorage.DeleteDocumentLibraryDocumentsAsync(documentlibraryid, false);

                    // delete documents for this document library from db
                    List <Models.Document> documents = Repository.Documents.GetAllFromDocumentLibrary(Convert.ToInt64(documentlibraryid));

                    foreach (Document d in documents)
                    {
                        var retVal = Repository.Documents.Delete(d);
                    }

                    // download documents from SharePoint to Azure Storage... this might take a while if files are large in size or number
                    SharePoint      sp = new SharePoint();
                    List <Document> syncedDocuments = await sp.DownloadFilesToAzure(docLib);

                    // Save documents that were downloaded from SharePoint and saved to Azure Blob Storage to SQL db
                    foreach (Document d in syncedDocuments)
                    {
                        long docId = Repository.Documents.Save(d);
                    }

                    // return successful response
                    return(Json(new
                    {
                        Success = true,
                        DocumentLibraryId = Convert.ToInt64(documentlibraryid)
                    }, JsonRequestBehavior.AllowGet));
                }
            }
            catch (Exception ex)
            {
                return(Json(new
                {
                    Success = false,
                    message = "An internal error occurred - please try again.  If this error persists, please contact your system administrator."
                }, JsonRequestBehavior.AllowGet));
            }

            return(Json(new
            {
                Success = false,
                message = "documentlibraryid is required."
            }, JsonRequestBehavior.AllowGet));
        }
Exemplo n.º 4
0
        public async Task <List <Models.Document> > DownloadFilesToAzure(Models.DocumentLibrary docLib)
        {
            List <Models.Document> documentsSaved = new List <Models.Document>();

            try
            {
                using (ClientContext context = new ClientContext(docLib.SiteUrl))
                {
                    ListItemCollection items = GetFilesRecursively(context, docLib);

                    // For each file returned, download the file
                    foreach (var item in items)
                    {
                        // Pull file information from SharePoint
                        string fileRef  = (string)item["FileRef"];
                        var    fileName = System.IO.Path.GetFileName(fileRef);
                        var    fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(context, fileRef);

                        // Construct the document object to save to the db
                        Models.Document newDocument = new Models.Document();
                        newDocument.Name              = fileName;
                        newDocument.Extension         = Path.GetExtension(fileName);
                        newDocument.DocumentLibraryId = docLib.DocumentLibraryId;
                        newDocument.AudienceId        = docLib.AudienceId;

                        //Console.WriteLine("File name => " + fileName);
                        //Console.WriteLine("Mime Type => " + MIMEAssistant.GetMIMEType(fileName));
                        //ClientResult<Stream> fileStream = file.OpenBinaryStream();
                        //Microsoft.SharePoint.Client.File.OpenBinaryDirect(context, fileRef);

                        // Save to Azure
                        AzureBlobStorage azureStorage = new AzureBlobStorage();
                        AzureFile        azureFile    = await azureStorage.UploadDocumentAsync(fileInfo.Stream, newDocument, docLib, fileName, MIMEAssistant.GetMIMEType(fileName));

                        // Set url to document object to save to db
                        newDocument.Url      = azureFile.Document.Url;
                        newDocument.Uploaded = DateTime.UtcNow;

                        documentsSaved.Add(newDocument);
                    }
                }

                return(documentsSaved);
            }
            catch (Exception e)
            {
                // TODO: add error handling here...
                Console.WriteLine("SharePoint.DownloadFilesToAzure debug, " + e.Message);
                return(null);
            }
        }
        public async Task <ActionResult> RemoveDocumentLibrary(string documentlibraryid)
        {
            try
            {
                if (documentlibraryid != null)
                {
                    Models.DocumentLibrary lib = new Models.DocumentLibrary()
                    {
                        DocumentLibraryId = Convert.ToInt64(documentlibraryid)
                    };

                    // delete files from Azure
                    AzureBlobStorage azureStorage = new AzureBlobStorage();
                    string           error        = await azureStorage.DeleteDocumentLibraryDocumentsAsync(documentlibraryid, true);

                    // delete documents for this document library from db
                    List <Models.Document> documents = Repository.Documents.GetAllFromDocumentLibrary(Convert.ToInt64(documentlibraryid));

                    foreach (Document d in documents)
                    {
                        var retVal = Repository.Documents.Delete(d);
                    }

                    // delete document library from db
                    long libId = Repository.DocumentLibrary.Delete(lib);

                    // return successful response
                    return(Json(new
                    {
                        Success = true,
                        DocumentLibraryId = Convert.ToInt64(documentlibraryid)
                    }, JsonRequestBehavior.AllowGet));
                }
            }
            catch (Exception ex)
            {
                return(Json(new
                {
                    Success = false,
                    message = "An internal error occurred - please try again.  If this error persists, please contact your system administrator."
                }, JsonRequestBehavior.AllowGet));
            }

            return(Json(new
            {
                Success = false,
                message = "documentlibraryid is required."
            }, JsonRequestBehavior.AllowGet));
        }
        /// <summary>
        /// Upload document from a document library
        /// </summary>
        /// <param name="fileStream"></param>
        /// <param name="document"></param>
        /// <param name="SPDocLibrary"></param>
        /// <param name="fileName"></param>
        /// <param name="contentType"></param>
        /// <returns></returns>
        public async Task <AzureFile> UploadDocumentAsync(Stream fileStream, Models.Document document, Models.DocumentLibrary SPDocLibrary, string fileName, string contentType)
        {
            string fileFullPath      = null;
            string blobContainerName = _DefaultAzureContainer;

            try
            {
                // NOTE: Azure Blob container names must be all lowercase and cannot start with numbers
                if (SPDocLibrary != null && SPDocLibrary.DocumentLibraryId != 0)
                {
                    blobContainerName = _SharePointDocLibPrefix + SPDocLibrary.DocumentLibraryId.ToString();
                }

                fileFullPath = await SaveAzureDocumentBlob(contentType, fileName, blobContainerName, fileStream);
            }
            catch (Exception ex)
            {
                // TODO: handle this exception and bubble up to the interface??
                return(new AzureFile(document, false, ex.Message));
            }

            document.Url      = fileFullPath;
            document.Uploaded = DateTime.UtcNow;

            return(new AzureFile(document, true, null));
        }