Example #1
0
        public static string GetError(Api2Error error)
        {
            var result = new Api2Result("error");

            result["error_id"]          = ((int)error).ToString();
            result["error_description"] = errorList[error];
            result["status"]            = Api2Result.status_error;

            return(JsonConvert.SerializeObject(result));
        }
Example #2
0
        private async Task Upload(HttpContext context, string cmd, UserItem userItem)
        {
            try
            {
                var boundary = context.Request.GetMultipartBoundary();

                if (string.IsNullOrWhiteSpace(boundary))
                {
                    await context.Response.WriteAsync(GetError(Api2Error.WrongRequest));

                    return;
                }

                string type = context.Request.Query["type"];
                string item = context.Request.Query["item"];

                // check type
                //Upload(string fileName, string description, Stream stream, string options)

                if (!DBManager.Instance.Get(type, out IManager manager))
                {
                    await context.Response.WriteAsync(GetError(Api2Error.Parameters));

                    return;
                }

                var reader  = new MultipartReader(boundary, context.Request.Body);
                var section = await reader.ReadNextSectionAsync();

                if (section == null)
                {
                    await context.Response.WriteAsync(GetError(Api2Error.Parameters));

                    return;
                }

                var fileSection = section.AsFileSection();
                var fileName    = fileSection.FileName;

                int id = await manager.Upload(userItem, fileSection.FileName, fileSection.FileStream, item);


                var result = new Api2Result(cmd);
                result["id"] = id;

                await context.Response.WriteAsync(JsonConvert.SerializeObject(result));
            }
            catch (Exception exc)
            {
                Logger.Instance.Save(exc);
                await context.Response.WriteAsync(GetError(Api2Error.Internal));
            }
        }
Example #3
0
        private async Task Auth(HttpContext context, string cmd, UserItem userItem)
        {
            if (!DBManager.Instance.Get("userinfo", out IManager manager))
            {
                await context.Response.WriteAsync(GetError(Api2Error.Parameters));

                return;
            }

            string login    = context.Request.Query["login"];
            string password = context.Request.Query["password"];

            if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password))
            {
                await context.Response.WriteAsync(GetError(Api2Error.Parameters));

                return;
            }

            var user = manager.Get("login", login) as UserInfo;

            if (user == null)
            {
                await context.Response.WriteAsync(GetError(Api2Error.Parameters));

                return;
            }

            if (string.Compare(user.password, password, true) != 0)
            {
                await context.Response.WriteAsync(GetError(Api2Error.Authorization));

                return;
            }

            ((UserManager)manager).UpdateActivity(user);

            var    result = new Api2Result(cmd);
            string token  = Guid.NewGuid().ToString("N");

            result["token"] = token;

            _activeUsers.Set(token, new UserItem()
            {
                uid = user.id, user = user
            });

            await context.Response.WriteAsync(JsonConvert.SerializeObject(result));
        }
Example #4
0
        private async Task Delete(HttpContext context, string cmd, UserItem userItem)
        {
            try
            {
                string body;
                using (var reader = new StreamReader(context.Request.Body))
                {
                    body = reader.ReadToEnd();
                }

                if (string.IsNullOrEmpty(body))
                {
                    await context.Response.WriteAsync(GetError(Api2Error.Parameters));

                    return;
                }

                string type = context.Request.Query["type"];
                // check type

                if (!DBManager.Instance.Get(type, out IManager manager))
                {
                    await context.Response.WriteAsync(GetError(Api2Error.Parameters));

                    return;
                }

                var del = Tools.Deserialize <Api2Delete>(body);
                if (del == null || del.id <= 0 || !manager.Delete(userItem, del.id))
                {
                    await context.Response.WriteAsync(GetError(Api2Error.WrongId));

                    return;
                }

                var result = new Api2Result(cmd);
                result["id"] = del.id;

                await context.Response.WriteAsync(JsonConvert.SerializeObject(result));
            }
            catch (Exception exc)
            {
                Logger.Instance.Save(exc);
                await context.Response.WriteAsync(GetError(Api2Error.Parameters));
            }
        }
Example #5
0
        private async Task Get(HttpContext context, string cmd, UserItem userItem)
        {
            try
            {
                string type = context.Request.Query["type"];

                int.TryParse(context.Request.Query["current_page"], out int current_page);
                int.TryParse(context.Request.Query["page_size"], out int page_size);

                string sort_by = context.Request.Query["sort_by"];
                bool.TryParse(context.Request.Query["descending"], out bool descending);

                bool dependence = Tools.ToInt(context.Request.Query["dependence"].ToString()) == 1;


                if (current_page <= 0)
                {
                    current_page = 1;
                }
                if (page_size <= 0)
                {
                    page_size = 5;
                }

                if (!DBManager.Instance.Get(type, out IManager manager))
                {
                    await context.Response.WriteAsync(GetError(Api2Error.Parameters));

                    return;
                }

                string filter = context.Request.Query["filter"];

                var filterList = Tools.Deserialize <List <FilterItem> >(filter);

                var items = manager.Get(userItem, current_page, page_size, out int total_items, sort_by, descending, filterList);

                var result = new Api2Result(cmd);

                var data = new Dictionary <string, object>()
                {
                    { "total_items", total_items },
                    { "current_page", current_page },
                    { "page_size", page_size },
                    { "items", items },
                };

                result["data"] = data;

                if (dependence)
                {
                    var dep = manager.Dependence(userItem, items);
                    if (dep != null)
                    {
                        data["dependence"] = dep;
                    }
                }


                //var result = new Dictionary<string, object>()
                //        {
                //            { "error_code", 0 },
                //            { "data", data },
                //        };

                await context.Response.WriteAsync(JsonConvert.SerializeObject(result));
            }
            catch (Exception exc)
            {
                Logger.Instance.Save(exc);
                await context.Response.WriteAsync(GetError(Api2Error.Parameters));
            }
        }