예제 #1
0
        public static async Task CheckinFromDrive(this Record record, string driveId, string token, bool saveRecord = false)
        {
            string downloadUrl = GraphApiHelper.GetOneDriveItemContentIdUrl(driveId);

            var fileResult = await ODataHelper.GetItem <OneDriveItem>(GraphApiHelper.GetOneDriveItemIdUrl(driveId), token, null);

            string filePath = Path.Combine(TrimApplication.WebServerWorkPath, fileResult.Name);


            await ODataHelper.GetItem <string>(downloadUrl, token, filePath);

            var inputDocument = new InputDocument(filePath);


            inputDocument.CheckinAs = record.SuggestedFileName;
            record.SetDocument(inputDocument, true, false, "checkin from Word Online");

            string pdfPath = Path.Combine(TrimApplication.WebServerWorkPath, Path.ChangeExtension(fileResult.Name, "pdf"));
            string pdfUrl  = GraphApiHelper.GetOneDriveItemContentIdUrl(driveId, "pdf");
            await ODataHelper.GetItem <string>(pdfUrl, token, pdfPath);


            var rendition = record.ChildRenditions.NewRendition(pdfPath, RenditionType.Longevity, "Preview");


            if (saveRecord)
            {
                record.Save();

                File.Delete(filePath);
                File.Delete(pdfPath);
            }
            return;
        }
예제 #2
0
        public async Task <Document> GetDocument(string token)
        {
            if (_recordUri < 1)
            {
                throw new ApplicationException("Invalid Uri");
            }

            if (_database == null || !_database.IsConnected)
            {
                throw new ApplicationException("Invalid database");
            }

            var response = new Document()
            {
                UserHasAccess = true
            };
            var record = new Record(_database, _recordUri);

            string driveId = record.GetDriveId();

            if (!string.IsNullOrWhiteSpace(driveId))
            {
                OneDriveItem fileResult = null;

                try
                {
                    fileResult = await ODataHelper.GetItem <OneDriveItem>(GraphApiHelper.GetOneDriveItemIdUrl(driveId), token, null);
                }
                catch (Exception ex)
                {
                    response.UserHasAccess = false;
                }

                if (response.UserHasAccess == false)
                {
                    token      = Tokens.getApplicationToken();
                    fileResult = await ODataHelper.GetItem <OneDriveItem>(GraphApiHelper.GetOneDriveItemIdUrl(driveId), token, null);
                }
                response.WebUrl    = fileResult.WebUrl;
                response.WebDavUrl = fileResult.WebDavUrl;
                response.MimeType  = fileResult.File.MimeType;
            }
            else if (record.IsElectronic)
            {
                try
                {
                    string folderId = string.Empty;

                    var documentFolder = await ODataHelper.PostFolder <OneDriveItem>(GraphApiHelper.GetOneDriveChildrenUrl(), token);

                    folderId = documentFolder.Id;

                    if (!record.IsDocumentInClientCache)
                    {
                        record.LoadDocumentIntoClientCache();
                    }

                    string fileName = record.GetFileName();

                    var uploadedFile = await doUpload(record.DocumentPathInClientCache, fileName, token);

                    bool checkout = true;
                    if (record.IsCheckedOut && record.CheckedOutTo.Uri == _database.CurrentUser.Uri)
                    {
                        checkout = false;
                    }


                    record.GetDocument(null, checkout, null, uploadedFile.ParentReference.DriveId + "/items/" + uploadedFile.Id);
                    record.SetDriveId(uploadedFile.ParentReference.DriveId + "/items/" + uploadedFile.Id);                    // uploadedFile. fileItem.getDriveAndId();

                    record.Save();


                    response.WebUrl    = uploadedFile.WebUrl;
                    response.WebDavUrl = uploadedFile.WebDavUrl;
                }
                catch
                {
                    try
                    {
                        record.UndoCheckout(null);
                    }
                    catch { }
                    //	return new Error
                    throw;
                }
            }
            else
            {
                throw new Exception("Record is not a valid document.");
            }

            return(response);
        }
예제 #3
0
        void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                Console.WriteLine("Running sync");
                BackgroundWorker w = (BackgroundWorker)sender;

                var token = Tokens.getApplicationToken();

                using (TrimHelper trimHelper = new TrimHelper())
                {
                    foreach (var doc in trimHelper.GetDeleteableDocuments())
                    {
                        Console.WriteLine($"rec: {doc.Id}");

                        try
                        {
                            if (!string.IsNullOrWhiteSpace(doc.Id))
                            {
                                var fileResult = ODataHelper.GetItem <OneDriveItem>(GraphApiHelper.GetOneDriveItemIdUrl(doc.Id), token, null);
                                fileResult.Wait();

                                var item = fileResult.Result;

                                var isLocked = ODataHelper.IsLocked(GraphApiHelper.GetOneDriveItemIdUrlForDelete(doc.Id), item.Name, token);
                                isLocked.Wait();

                                if (isLocked.Result == true)
                                {
                                    Console.WriteLine("Item is locked will try again later");
                                }
                                else
                                {
                                    var modified = doc.DateModified.ToUniversalTime();
                                    if (item.LastModifiedDateTime > modified)
                                    {
                                        trimHelper.CheckinFromDrive(doc, token);
                                    }

                                    StringContent content = new StringContent($"[TrimLink]{Environment.NewLine}Uri={doc.Uri}", Encoding.UTF8, "text/plain");
                                    string        url     = GraphApiHelper.GetOneDriveFileUploadUrlFromId(item.ParentReference.DriveId, item.ParentReference.Id, doc.LinkFileName);


                                    // delete original file
                                    var deleteResult = ODataHelper.DeleteWithToken(GraphApiHelper.GetOneDriveItemIdUrlForDelete(doc.Id), token);
                                    deleteResult.Wait();


                                    // Create link in Drive
                                    var uploadResult = ODataHelper.SendRequestWithAccessToken(url, token, content, method: HttpMethod.Put);
                                    uploadResult.Wait();

                                    trimHelper.ClearDriveId(doc);
                                    trimHelper.ResetDeleteNow(doc);

                                    Console.WriteLine(fileResult.Result.ParentReference.Id);
                                }
                            }
                            else
                            {
                                trimHelper.ResetDeleteNow(doc);
                            }
                        }
                        catch (Exception ex)
                        {
                            if (ex.InnerException != null)
                            {
                                Console.WriteLine(ex.InnerException.Message);
                                Console.WriteLine(ex.InnerException.StackTrace);
                            }
                            else
                            {
                                Console.WriteLine(ex.Message);
                                Console.WriteLine(ex.StackTrace);
                            }
                        }
                    }
                }

                //	while (/*condition*/)
                //	{
                //check if cancellation was requested
                if (w.CancellationPending)
                {
                    //take any necessary action upon cancelling (rollback, etc.)

                    //notify the RunWorkerCompleted event handler
                    //that the operation was cancelled
                    e.Cancel = true;
                    return;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }
            //report progress; this method has an overload which can also take
            //custom object (usually representing state) as an argument
            //	w.ReportProgress(/*percentage*/);

            //do whatever You want the background thread to do...
            //}
        }