/// <summary>
        /// Checks if a configuration exists. Will cache it if found.
        /// </summary>
        /// <typeparam name="T">The configuration type</typeparam>
        /// <typeparam name="Tm">The configuration metadata type</typeparam>
        /// <param name="configurationMetadata">The metadata related to the configuration, default gathered from Type Data</param>
        /// <returns>If the configuration exists</returns>
        protected internal virtual bool DoesConfigurationExist <T, Tm>(Tm configurationMetadata = default(Tm))
            where T : IConfiguration <T, Tm>, new()
            where Tm : IConfigurationMetadata <T, Tm>, new()
        {
            Tm metadata = GetMetadataForConfiguration <T, Tm>(configurationMetadata);

            return(CachingService.ContainsKey(metadata.ConfigurationName));
        }
        /// <summary>
        /// Removes a configuration from cache
        /// </summary>
        /// <typeparam name="T">The configuration type</typeparam>
        /// <typeparam name="Tm">The configuration metadata type</typeparam>
        /// <param name="configurationMetadata">The metadata related to the configuration, default gathered from Type Data</param>
        protected internal virtual void ClearConfiguration <T, Tm>(Tm configurationMetadata = default(Tm))
            where T : IConfiguration <T, Tm>, new()
            where Tm : IConfigurationMetadata <T, Tm>, new()
        {
            Tm metadata = GetMetadataForConfiguration <T, Tm>(configurationMetadata);

            if (CachingService.ContainsKey(metadata.ConfigurationName))
            {
                CachingService.Remove(metadata.ConfigurationName);
            }
        }
        /// <summary>
        /// Ensures that the request recognizes the correct time, and that it is not a repeat request (based on nonce recognition)
        /// </summary>
        /// <param name="contentStream">The stream of content to hash</param>
        /// <returns>If the request is a replay request</returns>
        protected virtual bool IsReplayRequest(string nonce, string requestTimeStamp)
        {
            if (string.IsNullOrWhiteSpace(nonce) || CachingService.ContainsKey(nonce))
            {
                return(true);
            }

            ulong serverTotalSeconds  = DateTime.UtcNow.UnixTimeStamp();
            ulong requestTotalSeconds = Convert.ToUInt64(requestTimeStamp);

            if ((serverTotalSeconds - requestTotalSeconds) > ServerClientMaxTimeDifference)
            {
                return(true);
            }

            DateTime cachedTime = requestTimeStamp?.GetDateTimeFromUnixTimestampString() ?? DateTime.MinValue;

            CachingService.Cache(nonce, nonce, cachedTime, cachedTime.AddSeconds(RequestMaxAgeInSeconds));

            return(false);
        }
Exemplo n.º 4
0
        //public ActionResult Test()
        //{
        //    string accessToken = "prE27SQYE58WyfmXx0RD6RQSqmKoHHxmGUQTojSgVsL0lV7g1RcI2DpA3ELJdNvdWNjFsR9kMbhC2Mht9bJDPva7ZRcRYRDV73DDUsYpa-YTZBcAIAPVX";
        //    MaterialApi.AddNormalMaterial(accessToken, "D:\\a.jpg", MaterialType.Image);

        //    return View();
        //}

        private FileUploadResult SaveFile()
        {
            FileUploadResult fileUploadResult = new FileUploadResult();

            HttpRequestBase request = this.HttpContext.Request;

            string strFileId = request.Form["txtFileUpload_Id"];

            if (String.IsNullOrEmpty(strFileId))
            {
                fileUploadResult.Message = "参数不正确。";
                return(fileUploadResult);
            }

            Guid fileId = Guid.Parse(strFileId);

            string cachingKey = "fsUploadResult:" + strFileId;

            if (_cachingService.ContainsKey(cachingKey))
            {
                fileUploadResult.Message = "请勿重复上传。";
                _cachingService.Set(cachingKey, fileUploadResult, _uploadResultExpiresIn);
                return(fileUploadResult);
            }

            //鉴权
            string userId = request.Form["txtFileUpload_UserId"];

            if (_controlledCachingService.Contains(userId) == false)
            {
                fileUploadResult.Message = "用户鉴权失败。";
                _cachingService.Set(cachingKey, fileUploadResult, _uploadResultExpiresIn);
                return(fileUploadResult);
            }

            if (request.Files.Count == 0)
            {
                fileUploadResult.Message = "没有要上传的文件。";
                _cachingService.Set(cachingKey, fileUploadResult, _uploadResultExpiresIn);
                return(fileUploadResult);
            }

            if (request.Files.Count > 1)
            {
                fileUploadResult.Message = "一次只能上传一个文件。";
                _cachingService.Set(cachingKey, fileUploadResult, _uploadResultExpiresIn);
                return(fileUploadResult);
            }

            HttpPostedFileBase file = request.Files[0];

            if (file.ContentLength / 1024 > 2048)
            {
                fileUploadResult.Message = String.Format("文件大小请勿超过:{0} KB", 2048);
                _cachingService.Set(cachingKey, fileUploadResult, _uploadResultExpiresIn);
                return(fileUploadResult);
            }

            FileInfo fileInfo = new FileInfo(file.FileName);

            SaveArgs args = new SaveArgs();

            args.Id            = fileId;
            args.Domain        = Guid.Parse(request.Form["txtFileUpload_Domain"]);
            args.FileName      = fileInfo.Name;
            args.FileExtension = fileInfo.Extension;
            //args.MD5 = request.Form["md5"];
            args.UploadDate      = DateTime.Now;
            args.UploadIPAddress = Request.UserHostAddress;
            args.Stream          = file.InputStream;

            //"D:\\WorkFloder\\Linkup\\trunk\\Source Code\\FileService\\Linkup.FileService\\File"
            //最后会有一个控制器的虚拟路径,所以 MapPath("/")
            //args.ServerRootDir = Server.MapPath("/");

            fileUploadResult = _fileManager.Save(args);

            _cachingService.Set(cachingKey, fileUploadResult, _uploadResultExpiresIn);

            return(fileUploadResult);
        }