Example #1
0
        public async Task SetKey()
        {
            var    client = new ClientInfomation(_httpContext);
            string val    = null;
            string refkey = CacheModel.ApiKey + client.GetClientID();
            var    value  = await _cache.GetAsync(refkey);

            if (value != null)
            {
                val = Encoding.UTF8.GetString(value);
            }
            else
            {
                val = CryptographyCore.SHA256_hash(Guid.NewGuid().ToString());
                await _cache.SetAsync(refkey, Encoding.UTF8.GetBytes(val));
            }
            if (_RequestServices != null)
            {
                _RequestServices.SetClientInfo(new ClientInfo()
                {
                    id           = refkey,
                    key          = val,
                    ipAddress    = client.GetClientIP(),
                    ConnectionID = client.GetConnectionID(),
                    UserAgent    = client.GetUserAgent()
                });
            }
            _httpContext.Response.Headers.Append("clientKey", val);
        }
Example #2
0
        public async Task Invoke(HttpContext httpContext, IRequestServices rq)
        {
            RequestServices = rq;

            var    client = new ClientInfomation(httpContext);
            string val    = null;
            string refkey = CacheModel.ApiKey + client.GetClientID();
            var    value  = await _cache.GetAsync(refkey);

            if (value != null)
            {
                val = Encoding.UTF8.GetString(value);
            }
            else
            {
                // load form db
                val = CryptographyCore.SHA256_hash(Guid.NewGuid().ToString());
                await _cache.SetAsync(refkey, Encoding.UTF8.GetBytes(val));

                // store to db
            }
            if (RequestServices != null)
            {
                RequestServices.SetClientInfo(new ClientInfo()
                {
                    id           = refkey, key = val, ipAddress = client.GetClientIP(),
                    ConnectionID = client.GetConnectionID(), UserAgent = client.GetUserAgent()
                });
            }
            httpContext.Response.Headers.Append("ClientKey", val);
            if (_next != null && httpContext != null)
            {
                await _next.Invoke(httpContext);
            }
        }
Example #3
0
        public async Task <ApiResultModel <FileStore_Images> > SaveImage(string mudule, IFormFile file, bool isPublic = false, int width = 0, int height = 0)
        {
            var result = new ApiResultModel <FileStore_Images>();

            if (mudule.isNOEOW())
            {
                result.BadRequest("Module name is required");
                return(result);
            }
            _module = mudule;
            try
            {
                if (file.Length > 0)
                {
                    var file_extension = Path.GetExtension(file.FileName);
                    var ext_data       = await _storage.GetRepository <IFileStore_Type_Repository>().ListAsync(x => x.file_type == File_Types.Image && x.is_active == true);

                    var check_type = ext_data.Where(x => x.file_extension == file_extension).FirstOrDefault();
                    if (check_type == null)
                    {
                        result.BadRequest($"File type not allowed, file must be ({(string.Join(",", ext_data.Select(x=>x.file_extension).ToArray()))})");
                        return(result);
                    }

                    string     filename = Guid.NewGuid().ToString() + file_extension;
                    string     url_img  = null;
                    string     path_img = null;
                    string     path     = @"\";
                    IFileProxy fileProxy;
                    if (isPublic && _appSetting.PublicDirectory != null)
                    {
                        path = $@"{_appSetting.PublicDirectory}\{_appSetting.FileStoreFolder}\{_module}\{ImagePath.Original}\";
                        if (!Directory.Exists(path))
                        {
                            Directory.CreateDirectory(path);
                        }
                        _fileStorage.CreateDirectoryProxy(path);
                        fileProxy = _fileStorage.CreateFileProxy(path, filename);
                        path_img  = $@"{_appSetting.FileStoreFolder}/{_module}/";
                        url_img   = $@"{path_img}{ImagePath.Thumbs}/{filename}";
                    }
                    else
                    {
                        path = $@"{AppContext.BaseDirectory}{_appSetting.FileStoreFolder}\{_module}\{ImagePath.Original}\";
                        if (!Directory.Exists(path))
                        {
                            Directory.CreateDirectory(path);
                        }
                        fileProxy = _fileStorage.CreateFileProxy(path, filename);
                        path_img  = $@"FileStore/Image/";
                        url_img   = $@"{path_img}{ImagePath.Thumbs}/{filename}";
                    }
                    await fileProxy.WriteStreamAsync(file.OpenReadStream());

                    new ImageResize(path + filename).ToThumnail(width, height);

                    if (url_img != null)
                    {
                        var data = await _storage.GetRepository <IFileStore_Images_Repository>().CreateAsync(new FileStore_Images()
                        {
                            file_type_id = check_type.id,
                            module       = _module,
                            title        = Path.GetFileNameWithoutExtension(file.FileName),
                            is_public    = isPublic,
                            name         = filename,
                            url          = url_img,
                            check_sum    = CryptographyCore.CreateCheckSUm_Stream(file.OpenReadStream())
                        });

                        result.Data = data;
                        return(result);
                    }
                    else
                    {
                        result.BadRequest();
                        return(result);
                    }
                }
                else
                {
                    result.BadRequest("File is required");
                    return(result);
                }
            }
            catch (Exception e)
            {
                result.BadRequest(e.Message);
                return(result);
            }
        }