コード例 #1
0
        public async Task <JsonResult> Put(string guid, decimal percentageRead)
        {
            string currentUserId = HttpContext.User.GetUserId();
            Dictionary <string, object> response = new Dictionary <string, object>();

            ApplicationUser user = await ControllerHelpers.GetCurrentUserAsync(_userManager, _dataAccess, currentUserId);

            if (user.EvernoteCredentials == null)
            {
                response["error"] = "You must authenticate with Evernote";
                return(Json(response));
            }

            IEvernoteService evernoteService = new EvernoteServiceSDK1(user.EvernoteCredentials);

            Bookmark bookmark = _dataAccess.GetAutomaticBookmark(currentUserId, guid);

            if (bookmark == null)
            {
                bookmark = new Bookmark()
                {
                    NoteGuid = guid,
                    Type     = BookmarkType.Automatic,
                    UserId   = currentUserId,
                    Updated  = DateTime.Now
                };
            }

            bookmark.PercentageRead = percentageRead;
            bookmark.Updated        = DateTime.Now;

            _dataAccess.SaveBookmark(bookmark);

            return(Json(response));
        }
コード例 #2
0
ファイル: ReaderController.cs プロジェクト: ksk100/EverReader
        public async Task <IActionResult> Read(string guid)
        {
            string          currentUserId = HttpContext.User.GetUserId();
            ApplicationUser user          = await ControllerHelpers.GetCurrentUserAsync(_userManager, _dataAccess, currentUserId);

            if (user.EvernoteCredentials == null)
            {
                return(View("MustAuthoriseEvernote"));
            }
            IEvernoteService evernoteService = new EvernoteServiceSDK1(user.EvernoteCredentials);

            Note note;

            try {
                note = evernoteService.GetNote(guid);
            }
            catch (EvernoteServiceSDK1AuthorisationException)
            {
                // thrown if the user's credentials are no longer valid
                return(View("EvernoteAuthorisationError"));
            }
            catch (EvernoteServiceSDK1NoteNotFoundException)
            {
                return(View("NoteNotFoundError"));
            }

            Bookmark bookmark = _dataAccess.GetAutomaticBookmark(currentUserId, guid);

            if (bookmark == null)
            {
                bookmark = new Bookmark()
                {
                    NoteGuid       = guid,
                    PercentageRead = 0,
                    UserId         = currentUserId,
                    Type           = BookmarkType.Automatic,
                    Updated        = DateTime.Now
                };
            }

            // update all the note metadata we store
            bookmark.NoteTitle   = note.Title;
            bookmark.NoteLength  = note.ContentLength;
            bookmark.NoteCreated = EvernoteSDKHelper.ConvertEvernoteDateToDateTime(note.Created);
            bookmark.NoteUpdated = EvernoteSDKHelper.ConvertEvernoteDateToDateTime(note.Updated);
            _dataAccess.SaveBookmark(bookmark);

            string decodedContent = WebUtility.HtmlDecode(note.Content);

            // ensure that links to other Evernote notes are directed through EverReader
            decodedContent = Regex.Replace(decodedContent,
                                           "<a href=\"evernote:///view/[^/]+/[^/]+/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})/",
                                           "<a href=\"/Reader/Read/$1");

            // convert en-media tags to img tags
            // get a list of image resources, indexed to the MD5SUM hash
            if (note.Resources != null)
            {
                Dictionary <string, string> hashGuidMapping = new Dictionary <string, string>();
                foreach (Resource resource in note.Resources)
                {
                    if (resource.Mime.StartsWith("image") && resource.Data.Size < 1024 * 512)
                    {
                        hashGuidMapping.Add(resource.Data.BodyHash.ToHexString(), resource.Guid);
                    }
                }
                // search and replace each resource in the document
                foreach (string hash in hashGuidMapping.Keys)
                {
                    decodedContent = Regex.Replace(decodedContent,
                                                   "en-media( [^>]*)? hash=[\"'](" + hash + ")[\"']",
                                                   "img $1 src=\"/Reader/NoteResource/" + hashGuidMapping[hash] + "\"");

                    decodedContent = Regex.Replace(decodedContent,
                                                   "<img([^>]*);height:auto;([^>]*)>",
                                                   "<img $1;$2>");
                }
            }

            ENNoteINoteMetadataAdapter erNoteMetaData = new ENNoteINoteMetadataAdapter(note);



            // Create view model for page
            ReaderViewModel readerViewModel = new ReaderViewModel()
            {
                FormattedNoteMetadata = new EverReaderNodeMetadataFormatter(erNoteMetaData),
                Content        = decodedContent,
                PercentageRead = bookmark == null ? 0 : bookmark.PercentageRead
            };

            return(View(readerViewModel));
        }