Beispiel #1
0
        public async Task <IActionResult> UploadMedia()
        {
            var @object = Request.Form["object"];
            var file    = Request.Form.Files["file"];

            var handler = ActivatorUtilities.CreateInstance <GetEntityMiddleware.GetEntityHandler>(_provider, User);
            var obj     = ASObject.Parse(@object);
            var mainObj = obj;

            if (obj["object"].Any())
            {
                mainObj = obj["object"].Single().SubObject;
            }

            var uploadPath = _configuration.GetSection("Kroeg")["FileUploadPath"];
            var uploadUri  = _configuration.GetSection("Kroeg")["FileUploadUrl"];

            var extension = file.FileName.Split('.').Last().Replace('/', '_');

            var fileName = Guid.NewGuid().ToString() + "." + extension;

            var str = System.IO.File.OpenWrite(uploadPath + fileName);
            await file.CopyToAsync(str);

            str.Dispose();

            mainObj.Replace("url", new ASTerm(uploadUri + fileName));

            if (obj["type"].Any(a => (string)a.Primitive == "Create"))
            {
                try
                {
                    obj = await handler.Post(HttpContext, (string)HttpContext.Items["fullPath"], obj);
                }
                catch (UnauthorizedAccessException e)
                {
                    return(StatusCode(403, e));
                }
                catch (InvalidOperationException e)
                {
                    return(StatusCode(401, e));
                }

                if (obj == null)
                {
                    return(NotFound());
                }
            }
            else
            {
                obj["id"].Clear();
                obj.Replace("attributedTo", new ASTerm(User.FindFirstValue(JwtTokenSettings.ActorClaim)));
                obj = (await _flattener.FlattenAndStore(_entityStore, obj)).Data;
                await _entityStore.CommitChanges();
            }

            obj = await _flattener.Unflatten(_entityStore, APEntity.From(obj, true));

            return(Content(obj.Serialize().ToString(), "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\""));
        }
Beispiel #2
0
        public async Task <IActionResult> UploadMedia()
        {
            var @object = Request.Form["object"];
            var file    = Request.Form.Files["file"];

            var handler = ActivatorUtilities.CreateInstance <GetEntityMiddleware.GetEntityHandler>(_provider, User);
            var obj     = ASObject.Parse(@object);
            var mainObj = obj;

            if (obj["object"].Any())
            {
                mainObj = obj["object"].Single().SubObject;
            }

            var uploadPath = _configuration.GetSection("Kroeg")["FileUploadPath"];
            var uploadUri  = _configuration.GetSection("Kroeg")["FileUploadUrl"];

            var extension = file.FileName.Split('.').Last().Replace('/', '_');

            var fileName = Guid.NewGuid().ToString() + "." + extension;

            var str = System.IO.File.OpenWrite(uploadPath + fileName);
            await file.CopyToAsync(str);

            str.Dispose();

            mainObj.Replace("url", ASTerm.MakePrimitive(uploadUri + fileName));

            var entity = await _entityStore.GetEntity((string)HttpContext.Items["fullPath"], false);

            try
            {
                using (var transaction = _connection.BeginTransaction())
                {
                    var entOut = await handler.Post(HttpContext, (string)HttpContext.Items["fullPath"], entity, obj);

                    if (entOut == null)
                    {
                        return(NotFound());
                    }
                    obj = await _flattener.Unflatten(_entityStore, entOut);

                    transaction.Commit();
                    return(Content(obj.Serialize().ToString(), "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\""));
                }
            }
            catch (UnauthorizedAccessException e)
            {
                Console.WriteLine(e);
                return(StatusCode(403, e));
            }
            catch (InvalidOperationException e)
            {
                Console.WriteLine(e);
                return(StatusCode(401, e));
            }
        }
Beispiel #3
0
            public async Task <ASObject> Parse(Stream request)
            {
                string data;

                using (var r = new StreamReader(request))
                    data = await r.ReadToEndAsync();

                return(ASObject.Parse(data, true));
            }
Beispiel #4
0
        public async Task <IActionResult> SharedInbox()
        {
            var userId = await _verifier.Verify(Request.Scheme + "://" + Request.Host.ToUriComponent() + Request.Path, HttpContext);

            if (userId == null)
            {
                return(Unauthorized());
            }
            var reader = new StreamReader(Request.Body);
            var data   = ASObject.Parse(await reader.ReadToEndAsync());

            if (!EntityData.IsActivity(data))
            {
                return(StatusCode(403, "Not an activity?"));
            }

            await _connection.OpenAsync();

            using (var transaction = _connection.BeginTransaction())
            {
                APEntity resultEntity;
                if (data["actor"].Any((a) => a.Id == userId))
                {
                    var temporaryStore = new StagingEntityStore(_entityStore);
                    resultEntity = await _entityFlattener.FlattenAndStore(temporaryStore, data, false);

                    await temporaryStore.TrimDown((new Uri(new Uri(userId), "/")).ToString());

                    await temporaryStore.CommitChanges();
                }
                else
                {
                    resultEntity = await _entityStore.GetEntity(data.Id, true);

                    if (resultEntity == null)
                    {
                        return(StatusCode(202));
                    }
                    data = resultEntity.Data;
                }

                var users = await _deliveryService.GetUsersForSharedInbox(data);

                foreach (var user in users)
                {
                    await DeliverToActivityPubTask.Make(new DeliverToActivityPubData { ObjectId = resultEntity.Id, TargetInbox = user.Data["inbox"].First().Id }, _connection);
                }

                transaction.Commit();
                return(StatusCode(202));
            }
        }
Beispiel #5
0
        public async Task <IActionResult> PostEntity(string id)
        {
            string data;

            using (var reader = new StreamReader(Request.Body))
                data = await reader.ReadToEndAsync();
            var entity = await _entityStore.GetEntity(id, true);

            if (entity == null)
            {
                return(NotFound());
            }

            entity.Data = ASObject.Parse(data);
            await _entityStore.StoreEntity(entity);

            return(Ok());
        }
Beispiel #6
0
        public async Task <IActionResult> SharedInbox()
        {
            var userId = await _verifier.Verify(Request.Scheme + "://" + Request.Host.ToUriComponent() + Request.Path, HttpContext);

            if (userId == null)
            {
                return(Unauthorized());
            }
            var reader = new StreamReader(Request.Body);
            var data   = ASObject.Parse(await reader.ReadToEndAsync());

            if (!_entityConfiguration.IsActivity(data))
            {
                return(StatusCode(403, "Not an activity?"));
            }
            if (!data["actor"].Any((a) => (string)a.Primitive == userId))
            {
                return(StatusCode(403, "Invalid signature!"));
            }

            var temporaryStore = new StagingEntityStore(_entityStore);
            var resultEntity   = await _entityFlattener.FlattenAndStore(temporaryStore, data, false);

            temporaryStore.TrimDown((new Uri(new Uri(userId), "/")).ToString());
            await temporaryStore.CommitChanges(); // shouuuuld be safe

            var users = await _deliveryService.GetUsersForSharedInbox(data);

            foreach (var user in users)
            {
                if (user.IsOwner)
                {
                    _context.EventQueue.Add(DeliverToActivityPubTask.Make(new DeliverToActivityPubData {
                        ObjectId = resultEntity.Id, TargetInbox = (string)user.Data["inbox"].First().Primitive
                    }));
                }
            }

            await _context.SaveChangesAsync();

            return(StatusCode(202));
        }