Beispiel #1
0
        internal static void RebuildDB(string userID)
        {
            DBClient.ListDatabaseNames().ForEachAsync(s => Console.WriteLine(s));
            var files = Directory.GetFiles(BASE_DIR, "", SearchOption.AllDirectories);

            Console.WriteLine(files.Length);
            for (int i = 0; i < files.Length; i++)
            {
                var media = new MediaModel
                {
                    id       = Path.GetFileNameWithoutExtension(files[i]),
                    fileName = Path.GetFileName(files[i]),
                    type     = MediaModel.GetMediaType(files[i])
                };
                Console.WriteLine($"Writing to DB: {media.id} | {media.type} ");
                Media.InsertOne(new BsonDocument
                {
                    { "id", media.id },
                    { "type", media.type },
                    //{ "media", (useDB ? mediaId : BsonValue.Create(null) )},
                    { "mediaUri", $"{media.type.ToString()}/{media.fileName}" },
                    //{ "mediaBin", (useDB ? BsonValue.Create(null) : new BsonBinaryData(StreamToByteA(media.mediaStream))) },
                    { "fileName", media.fileName },
                    { "owner", userID },
                    { "views", 0 }
                });
            }
            Console.ReadLine();
        }
Beispiel #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
                });
            });
        }