예제 #1
0
        public RegistrationModule() : base("register")
        {
            base.Get("/{token}", (Func <dynamic, object>)(p =>
            {
                var referer = AobaCore.ValidateRegistrationToken(((string)p.token).Replace(' ', '+'));
                if (referer == null || referer == UserModel.Overlord)
                {
                    return new Response {
                        StatusCode = HttpStatusCode.Unauthorized
                    }
                }
                ;
                else
                {
                    return(View["register", new { referer = referer.Username }]);
                }
            }));

            Post("/{token}", p =>
            {
                var userInfo = this.Bind <LoginCredentialsModel>();
                var token    = (string)p.token;
                if (!string.IsNullOrWhiteSpace(token) && AobaCore.RegisterUser(userInfo, token.Replace(' ', '+')))
                {
                    return(new Response {
                        StatusCode = HttpStatusCode.OK
                    });
                }
                else
                {
                    return new Response {
                        StatusCode = HttpStatusCode.Unauthorized
                    }
                };
            });
        }
예제 #2
0
        public APIModule() : base("/api")
        {
            StatelessAuthentication.Enable(this, AobaCore.StatelessConfig);
            Before.AddItemToEndOfPipeline(ctx =>
            {
                return((this.Context.CurrentUser == null) ? new HtmlResponse(HttpStatusCode.Unauthorized) : null);
            });

            Get("/regToken", p =>
            {
                var user = Context.CurrentUser as UserModel;
                return(AobaCore.GetNewRegToken(user.ID));
            });

            Get("/userStats", _ =>
            {
                var uid = ((UserModel)Context.CurrentUser).ID;
                return(Response.AsJson(AobaCore.GetUserStats(uid)).WithHeader("Authorization", $"Bearer {AobaCore.GetJWT(AobaCore.GetApiKey(uid), 365)}"));
            });

            Get("/", _ =>
            {
                return(new Response {
                    StatusCode = HttpStatusCode.OK
                });
            });

            Post("/image", p =>
            {
                try
                {
                    var f     = Context.Request.Files.First();
                    var media = new MediaModel
                    {
                        type        = MediaModel.GetMediaType(f.Name),
                        mediaStream = f.Value,
                        fileName    = f.Name
                    };

                    media.mediaStream.Position = 0;
                    if (string.IsNullOrEmpty(media.Ext))
                    {
                        var ext        = GetImageExt(media.mediaStream);
                        media.fileName = $"{media.fileName}{ext}";
                        media.type     = MediaModel.GetMediaType(ext);
                    }
                    var uid = ((UserModel)Context.CurrentUser).ID;
                    AobaCore.AddMedia(uid, media);
                    var response = string.Empty;
                    if (media.type == MediaModel.MediaType.Raw)
                    {
                        response = $"{AobaCore.HOST}/i/raw/{media.id}/{media.fileName}";
                    }
                    else if (media.Ext == ".gif")
                    {
                        response = $"{AobaCore.HOST}/i/raw/{media.id}/{media.fileName}";
                    }
                    else
                    {
                        response = $"{AobaCore.HOST}/i/{media.id}";
                    }
                    if (Context.Request.Headers.AcceptEncoding.Contains("JSON"))
                    {
                        return(Response.AsJson(new
                        {
                            id = media.id,
                            url = response
                        }));
                    }
                    return(Response.AsText(response).WithHeader("Authorization", $"Bearer {AobaCore.GetJWT(AobaCore.GetApiKey(uid), 365)}"));
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.StackTrace);
                    return(new Response()
                    {
                        StatusCode = HttpStatusCode.ImATeapot
                    });
                }
            });

            Delete("image/{id}", p =>
            {
                AobaCore.DeleteImage(p.id);
                return(new Response()
                {
                    StatusCode = HttpStatusCode.OK
                });
            });
        }
예제 #3
0
        public MediaModule() : base("/i")
        {
            Get("/{id}", p =>
            {
                var id    = (string)p.id;
                id        = id.Replace(' ', '+');
                var start = DateTime.Now;
                var media = AobaCore.GetMedia(id);
                Console.WriteLine($"Retreive Latency: {(DateTime.Now - start).TotalMilliseconds}ms");
                if (media == null)
                {
                    return(new NotFoundResponse());
                }
                else
                {
                    if (media.mediaStream == null || media.mediaStream.Length <= 0)
                    {
                        return(new NotFoundResponse());
                    }
                    string ext = media.Ext;
                    AobaCore.IncrementViewCount(media.id);
                    switch (media.type)
                    {
                    //Image
                    case MediaType.Image:
                        return(Response.FromStream(media.mediaStream, MimeTypes.GetMimeType(ext)));

                    //Text
                    case MediaType.Text:
                        return(Response.FromStream(media.mediaStream, "text/plain"));

                    //Code
                    case MediaType.Code:
                        return(Response.FromStream(media.mediaStream, "text/plain"));                                // TODO: Code View

                    //return View["code.cshtml", new { code = File.ReadAllText(uri) }];
                    //Audio
                    case MediaType.Audio:
                        try
                        {
                            var file = TagLib.File.Create(new FileStreamAbstraction($"{media.id}{ext}", media.mediaStream));
                            return(View["audio.cshtml", new { p.id, rawUri = $"/i/raw/{id}/{media.fileName}", format = ext, title = file.Tag.Title, artist = (file.Tag.FirstPerformer ?? file.Tag.AlbumArtists.First()), album = file.Tag.Album }]);
                        }
                        catch (TagLib.UnsupportedFormatException)
                        {
                            return(View["audio.cshtml", new { p.id, rawUri = $"/i/raw/{id}/{media.fileName}", format = ext }]);
                        }

                    //Video
                    case MediaType.Video:
                        return(Response.FromStream(media.mediaStream, MimeTypes.GetMimeType(media.Ext)));                                // TODO: Video player

                    //return View["video.cshtml", new { rawUri = $"/i/raw/{(string)p.id}{ext}", format = Path.GetExtension(media.uri).ToLower() }];
                    //Raw
                    default:
                        return(Response.AsRedirect($"/i/raw/{id}/{media.fileName}"));
                    }
                }
            });

            Get("/{id}/og", p =>
            {
                var id    = (string)p.id;
                id        = id.Replace(' ', '+');
                var media = AobaCore.GetMedia(id);
                if (media == null)
                {
                    return(new NotFoundResponse());
                }
                else
                {
                    if (media.mediaStream == null || media.mediaStream.Length <= 0)
                    {
                        return(new NotFoundResponse());
                    }
                    string ext = media.Ext;
                    switch (media.type)
                    {
                    //Image
                    case MediaType.Image:
                        return(Response.FromStream(media.mediaStream, MimeTypes.GetMimeType(ext)));

                    //Text
                    case MediaType.Text:
                        return(new NotFoundResponse());

                    //Code
                    case MediaType.Code:
                        return(new NotFoundResponse());

                    //Audio
                    case MediaType.Audio:
                        try
                        {
                            var file = TagLib.File.Create(new FileStreamAbstraction($"{media.id}{ext}", media.mediaStream));
                            if (file.Tag.Pictures.Length == 0)
                            {
                                return(new NotFoundResponse());
                            }
                            return(Response.FromStream(new MemoryStream(file.Tag.Pictures.First().Data.Data), "image/png"));
                        }
                        catch (TagLib.UnsupportedFormatException)
                        {
                            return(new NotFoundResponse());
                        }

                    //Video
                    case MediaType.Video:
                        return(new NotFoundResponse());

                    default:
                        return(Response.AsRedirect($"/i/raw/{id}/{media.fileName}"));
                    }
                }
            });

            Get("/raw/{id}/{fName}.{ext}", p =>
            {
                var media = AobaCore.GetMedia(((string)p.id).Replace(' ', '+'));
                if (media == null)
                {
                    return(new NotFoundResponse());
                }
                else
                {
                    return(Response.FromStream(media.mediaStream, MimeTypes.GetMimeType(media.Ext)));
                }
            });

            //Get["/"] = _ =>
            //{
            //return new NotFoundResponse();
            //};
        }
예제 #4
0
파일: AuthModule.cs 프로젝트: Amatsugu/Aoba
        public AuthModule() : base("/auth")
        {
            Post("/login", p =>
            {
                LoginCredentialsModel user = this.Bind <LoginCredentialsModel>();
                if (user == null)
                {
                    return new Response {
                        StatusCode = HttpStatusCode.Unauthorized
                    }
                }
                ;
                string apiKey = AobaCore.ValidateUser(user);
                if (apiKey == null)
                {
                    return new Response {
                        StatusCode = HttpStatusCode.Unauthorized
                    }
                }
                ;
                if (user.AuthMode == AuthMode.API)
                {
                    return(Response.AsJson(new { jwt = AobaCore.GetJWT(apiKey, 365) }));
                }
                else
                {
                    var token = AobaCore.GetJWT(apiKey);
                    return(new Response().WithHeader("Authorization", $"Bearer {token}").WithCookie("token", token));
                }
            });

            Get("/logout", _ =>
            {
                return(new Response().WithCookie("token", ""));
            });

            Post("/register/{token}", p =>
            {
                LoginCredentialsModel user = this.Bind <LoginCredentialsModel>();
                var token = (string)p.token;
                if (!string.IsNullOrWhiteSpace(token) && AobaCore.RegisterUser(user, token.Replace(' ', '+')))
                {
                    return(new Response {
                        StatusCode = HttpStatusCode.OK
                    });
                }
                else
                {
                    return new Response {
                        StatusCode = HttpStatusCode.Unauthorized
                    }
                };
            });

            Post("/checkuser", p =>
            {
                return((AobaCore.UserExists(Request.Body.AsString())) ? new Response {
                    StatusCode = HttpStatusCode.NotAcceptable
                } : new Response {
                    StatusCode = HttpStatusCode.OK
                });
            });
        }