Пример #1
0
        public async Task <object> Get(OpenFile request)
        {
            if (request.Uri < 1)
            {
                throw new Exception("Invalid Uri");
            }

            var response = new OpenFileResponse();
            var record   = new Record(this.Database, request.Uri);

            string token = await getToken();

            string driveId = record.ExternalReference;


            OneDriveItem fileResult = null;

            if (!string.IsNullOrWhiteSpace(driveId))
            {
                fileResult = await ODataHelper.GetItem <OneDriveItem>(GraphApiHelper.GetOneDriveItemIdUrl(driveId), token, null);

                response.WebUrl = fileResult.WebUrl;
            }
            else if (record.IsElectronic)
            {
                string folderId = string.Empty;

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

                    folderId = documentFolder.Id;

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

                    var fileItem = await ODataHelper.PostFile <OneDriveItem>(GraphApiHelper.GetOneDriveFileUploadUrl(folderId, record.SuggestedFileName), token, record.DocumentPathInClientCache);

                    record.ExternalReference = fileItem.Id;
                    record.Save();
                    record.GetDocument(null, true, null, null);

                    response.WebUrl = fileItem.WebUrl;
                }
                catch
                {
                    throw;
                }
            }
            else
            {
                throw new Exception("Record is not a valid document.");
            }
            return(response);
        }
Пример #2
0
        public async Task <object> Get(OpenFile request)
        {
            if (request.Uri < 1)
            {
                throw new HttpError(HttpStatusCode.BadRequest, "400", "Invalid Uri");
            }

            var response = new OpenFileResponse()
            {
                UserHasAccess = true
            };
            var record = new Record(this.Database, request.Uri);

            string driveId = record.SpURL;

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

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

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

                    string folderId = string.Empty;

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

                    folderId = documentFolder.Id;

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

                    Regex pattern = new Regex("[\\\\/<>|?]|[\n]{2}");

                    string fileName = $"{Path.GetFileNameWithoutExtension(record.SuggestedFileName)} ({pattern.Replace(record.Number, "_")}){Path.GetExtension(record.SuggestedFileName)}";



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

                    record.GetDocument(null, true, null, uploadedFile.ParentReference.DriveId + "/items/" + uploadedFile.Id);
                    record.SpURL = 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);
        }