Exemplo n.º 1
0
        public void CreatTokenTest()
        {
            WebSiteConfig postgreSQLConfig = ConfigManager.GetPostgreSQLConfig();
            UpYun         upyun            = new UpYun("bucket", "username", "password");

            upyun.secret = postgreSQLConfig.upyunSecret;;
            Int32 unixTimestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;

            unixTimestamp += 120;
            string _upt = upyun.CreatToken(unixTimestamp.ToString(), upyun.secret, $"/upload/debbb8740a597ca554d194818af6e560/270a9d65cfb903fb89cb21c78bd6bad106b543ca.jpg");

            Console.WriteLine($"https://upyun.morenote.top/upload/debbb8740a597ca554d194818af6e560/270a9d65cfb903fb89cb21c78bd6bad106b543ca.jpg?_upt={_upt}");
        }
Exemplo n.º 2
0
        public async Task <IActionResult> GetRandomImage(string type)
        {
            int hour = DateTime.Now.Hour;

            lock (_fuseObj)
            {
                _fuseCount++;
            }
            string ext = null;

            if (string.IsNullOrEmpty(type))
            {
                type = "动漫综合2";
            }
            if (type.Equals("少女映画"))
            {
                string userHex = HttpContext.Session.GetString("_userId");
                if (string.IsNullOrEmpty(userHex))
                {
                    //没登陆
                    return(Redirect("/Auth/login"));
                }
            }
            if (!_randomImageList.ContainsKey(type))
            {
                _randomImageList.Add(type, new List <RandomImage>(size));
            }
            RandomImage randomImage = RandomImageService.GetRandomImage(type);

            if (_randomImageList[type].Count < size)
            {
                randomImage = RandomImageService.GetRandomImage(type);
                if (random == null)
                {
                    return(NotFound());
                }
                _randomImageList[type].Add(randomImage);
            }
            else
            {
                if (DateTime.Now.Hour != _initTime)
                {
                    _fuseCount = 0;
                    _initTime  = DateTime.Now.Hour;
                    _randomImageList[type].Clear();
                    randomImage = RandomImageService.GetRandomImage(type);
                    if (random == null)
                    {
                        return(NotFound());
                    }
                    _randomImageList[type].Add(randomImage);
                }
                else
                {
                    if (_fuseCount > _randomImageFuseSize)
                    {
                        Response.StatusCode = (int)HttpStatusCode.BadGateway;
                        return(Content("接口并发太高,接口已经熔断"));
                    }
                    int index = random.Next(_randomImageList[type].Count - 1);
                    randomImage = _randomImageList[type][index];
                }
            }

            if (randomImage == null)
            {
                return(NotFound());
            }
            ext = Path.GetExtension(randomImage.FileName);
            var           headers       = Request.Headers;
            StringBuilder stringBuilder = new StringBuilder();

            foreach (var item in headers)
            {
                stringBuilder.Append(item.Key + "---" + item.Value + "\r\n");
            }
            string        RealIP        = headers["X-Forwarded-For"].ToString().Split(",")[0];
            AccessRecords accessRecords = new AccessRecords()
            {
                AccessId        = SnowFlake_Net.GenerateSnowFlakeID(),
                IP              = RealIP,
                X_Real_IP       = headers["X-Real-IP"],
                X_Forwarded_For = headers["X-Forwarded-For"],
                Referrer        = headers["Referer"],
                RequestHeader   = stringBuilder.ToString(),
                AccessTime      = DateTime.Now,
                UnixTime        = (long)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds,
                TimeInterval    = -1,
                url             = "/api/GetRandomImage"
            };
            await AccessService.InsertAccessAsync(accessRecords).ConfigureAwait(false);

            type         = randomImage.TypeNameMD5;
            upyun.secret = postgreSQLConfig.upyunSecret;;
            Int32 unixTimestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;

            unixTimestamp += 3;
            //开启token防盗链

            if (postgreSQLConfig.token_anti_theft_chain)
            {
                string _upt = upyun.CreatToken(unixTimestamp.ToString(), upyun.secret, $"/upload/{type}/{randomImage.FileSHA1}{ext}");
                return(Redirect($"https://upyun.morenote.top/upload/{type}/{randomImage.FileSHA1}{ext}?_upt={_upt}"));
            }
            else
            {
                return(Redirect($"https://upyun.morenote.top/upload/{type}/{randomImage.FileSHA1}{ext}"));
            }
        }