/// <summary>
        /// 修改 Url 内容
        /// </summary>
        /// <param name="id"></param>
        /// <param name="url"></param>
        /// <param name="title"></param>
        /// <returns></returns>

        public async Task ModifyCollection(int id, string url, string title, int?typeId = null)
        {
            var userId = GetUserId();

            try
            {
                var collection = await loveCollectionAppService.CollectionQuery(userId)
                                 .Where(t => t.Id == id)
                                 .FirstOrDefaultAsync();

                collection.Url   = url;
                collection.Title = title;
                if (typeId.HasValue)
                {
                    collection.TypeId = typeId.Value;
                }
                await _collectionDBCotext.SaveChangesAsync();
            }
            catch (Exception)
            {
                await loveCollectionAppService.UpdateAllTypeToRedisAsync(userId);

                throw;
            }
            finally
            {
                await loveCollectionAppService.UpdateAllCollectionToRedisAsync(userId);
            }
        }
예제 #2
0
        public async Task <int> AddType(string name, string userToken = null)
        {
            var userId   = GetUserId(userToken);
            var typeSort = 0.0;

            if (await loveCollectionAppService.TypeQuery(userId).AnyAsync())
            {
                typeSort = await loveCollectionAppService.TypeQuery(userId).MaxAsync(t => t.Sort);
            }
            var type = _collectionDBCotext.Types.Add(new Entities.Type()
            {
                Name   = name,
                UserId = userId,
                Sort   = typeSort + 1024
            });
            await _collectionDBCotext.SaveChangesAsync();

            await loveCollectionAppService.UpdateAllTypeToRedisAsync(userId);

            return(type.Entity.Id);
        }
예제 #3
0
        /// <summary>
        /// 导入书签
        /// </summary>
        /// <returns></returns>
        public async Task <IActionResult> ImportBookmark(IFormFile file)
        {
            var userId = GetUserId();

            if (userId == 0)
            {
                return(Redirect("/"));
            }
            using (var stream = file.OpenReadStream())
            {
                byte[] buffer = new byte[stream.Length];
                await stream.ReadAsync(buffer, 0, (int)stream.Length);

                var        htmlString = Encoding.UTF8.GetString(buffer);
                HtmlParser htmlParser = new HtmlParser();
                var        urls       = await loveCollectionAppService.GetCollectionUrlsByUserIdAsync(userId);

                var tempTypeId = await loveCollectionAppService.GetOrAddTypeIdByUserIdAsync("未分类", userId);

                #region  类型导入
                foreach (var childNode in htmlParser.Parse(htmlString).QuerySelector("DL DL").ChildNodes)
                {
                    if (childNode is AngleSharp.Dom.IElement)
                    {
                        var element  = childNode as AngleSharp.Dom.IElement;
                        var typeName = element.QuerySelectorAll("H3").FirstOrDefault()?.TextContent;
                        var typeId   = tempTypeId;
                        if (!string.IsNullOrWhiteSpace(typeName))
                        {
                            typeId = await loveCollectionAppService.GetOrAddTypeIdByUserIdAsync(typeName, userId);
                        }
                        var collections = element.QuerySelectorAll("A").ToList();
                        foreach (var collection in collections)
                        {
                            var url = collection.Attributes.FirstOrDefault(f => f.Name == "href")?.Value;
                            url = url.Length >= 500 ? url.Substring(0, 500) : url;
                            if (urls.Contains(url))//忽略 已经存在 或 已经被导入过的链接
                            {
                                continue;
                            }
                            var value = collection.TextContent;
                            value = value.Length >= 300 ? value.Substring(0, 300) : value;
                            await loveCollectionAppService.SaveCollectionAsync(value, url, typeId, userId);

                            urls.Add(url);
                        }
                        await loveCollectionAppService.SaveChangesAsync();
                    }
                }
                #endregion

                #region 重新检测漏网之鱼
                foreach (var collection in htmlParser.Parse(htmlString).QuerySelectorAll("A"))
                {
                    var url = collection.Attributes.FirstOrDefault(f => f.Name == "href")?.Value;
                    url = url.Length >= 500 ? url.Substring(0, 500) : url;
                    if (urls.Contains(url))//忽略 已经存在 或 已经被导入过的链接
                    {
                        continue;
                    }
                    var value = collection.TextContent;
                    value = value.Length >= 300 ? value.Substring(0, 300) : value;
                    await loveCollectionAppService.SaveCollectionAsync(value, url, tempTypeId, userId);

                    urls.Add(url);
                }
                await loveCollectionAppService.SaveChangesAsync();

                #endregion
            }
            await loveCollectionAppService.UpdateAllCollectionToRedisAsync(userId);

            await loveCollectionAppService.UpdateAllTypeToRedisAsync(userId);

            return(View());
        }