예제 #1
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);
        }