예제 #1
0
        public JukeBoxModule(DropboxHelper dropboxHelper)
            : base("jukebox")
        {
            Get["/play/{id}"] = _ =>
            {
                return View["jukebox/play"];
            };

            Get["/download/{id}"] = _ =>
            {
                //Buscamos los urls de miusiquita
                //var filesName = new List<string>
                //{
                //	"Miusiquita/01 No es en serio.mp3",
                //	"Miusiquita/02 Bla bla bla.mp3",
                //};
                //var files = new List<Tuple<string, byte[]>>();

                //foreach (var item in filesName)
                //{
                //	var bytes = dropboxHelper.GetFile(item);
                //	files.Add(new Tuple<string, byte[]>(Path.GetFileName(item), bytes));
                //}

                //using (var zipStream = new MemoryStream())
                //{
                //	using (ZipFile zip = new ZipFile())
                //	{
                //		foreach (var item in files)
                //		{
                //			zip.AddEntry(item.Item1, item.Item2);
                //		}

                //		zip.Save(zipStream);

                //		var responseStream = new MemoryStream(zipStream.ToArray());
                //		return Response.FromStream(responseStream, "application/zip").AsAttachment("play.zip");
                //	}
                //}

                var f = File.ReadAllBytes(@"C:\tmp\a.zip");

                var responseStream = new MemoryStream(f);
                return Response.FromStream(responseStream, "application/zip").AsAttachment("play.zip");

            };
        }
예제 #2
0
        public RoomModule(DropboxHelper dropboxHelper)
            : base("rooms")
        {
            _dropboxHelper = dropboxHelper;

            Get["/play/{id}"] = _ =>
            {
                return View["jukebox/play"];
            };

            Get["/download/{id}"] = p =>

            {
                if (!p.id.HasValue)
                    return HttpStatusCode.NotFound;

                var roomId = string.Format("{0}/{1}", this.ModulePath, p.id);
                Room room = RavenSession.Load<Room>(roomId);
                var user = RavenSession.Load<User>(room.User.Id);

                _dropboxHelper.Client.UserLogin = new DropNet.Models.UserLogin { Secret = user.DropboxApiUserSecret, Token = user.DropboxApiUserToken };

                var filesName = _dropboxHelper.GetFiles(room.DropboxPath);

                var files = new List<Tuple<string, byte[]>>();

                foreach (var item in filesName)
                {
                    var bytes = _dropboxHelper.GetFile(item);
                    files.Add(new Tuple<string, byte[]>(Path.GetFileName(item), bytes));
                }

                using (var zipStream = new MemoryStream())
                {
                    using (ZipFile zip = new ZipFile())
                    {
                        foreach (var item in files)
                        {
                            zip.AddEntry(item.Item1, item.Item2);
                        }

                        zip.Save(zipStream);

                        var responseStream = new MemoryStream(zipStream.ToArray());
                        return Response.FromStream(responseStream, "application/zip").AsAttachment("play.zip");
                    }
                }
            };

            Get["/all"] = _ =>
            {
                var m = Context.Model("Rooms");
                m.RoomActive = "active";

                return View["room/all.html", m];
            };

            Get["/all-rooms"] = _ =>
            {
                int skip = (Request.Query.queries.HasValue ? Request.Query.skip : 0);
                int take = (Request.Query.queries.HasValue ? Request.Query.take : 10);

                var rooms = RavenSession.Query<Room>()
                                            .Skip(skip)
                                            .Take(take)
                                            .ToList();

                return rooms;
            };

            Get["/{id}"] = p =>
            {
                if(!p.id.HasValue)
                    return HttpStatusCode.NotFound;

                //var filesName = dropboxHelper.GetFiles(room.DropboxPath);

                var roomId = string.Format("{0}/{1}", this.ModulePath, p.id);
                Room room = RavenSession.Load<Room>(roomId);
                var user = RavenSession.Load<User>(room.User.Id);

                if (room == null)
                    return HttpStatusCode.NotFound;

                _dropboxHelper.Client.UserLogin = new DropNet.Models.UserLogin { Token = user.DropboxApiUserToken, Secret = user.DropboxApiUserSecret };

                var m = Context.Model("Room " + room.Name);
                m.MyRoomActive = "active";
                m.RoomId = room.Id;
                m.RoomName = room.Name;
                m.Files = (dropboxHelper.Client.UserLogin != null ? dropboxHelper.GetFiles(room.DropboxPath).ToList() : room.Songs);
                m.IsOwner = room.User.Id == Context.UserRavenIdString();

                return View["room/view.html", m];
            };

            Get["/modified-date/{id}"] = p =>
            {
                if (!p.id.HasValue)
                    return HttpStatusCode.NotFound;

                var roomId = string.Format("{0}/{1}", this.ModulePath, p.id);
                Room room = RavenSession.Load<Room>(roomId);
                var user = RavenSession.Load<User>(room.User.Id);

                _dropboxHelper.Client.UserLogin = new DropNet.Models.UserLogin { Secret = user.DropboxApiUserSecret, Token = user.DropboxApiUserToken };
                var metaData = _dropboxHelper.Client.GetMetaData(room.DropboxPath);

                return metaData.ModifiedDate;
            };
        }
예제 #3
0
        public RoomModuleSecure(DropboxHelper dropboxHelper)
            : base("rooms")
        {
            this.RequiresAuthentication();

            _dropboxHelper = dropboxHelper;

            Get["/my"] = _ =>
            {
                var m = Context.Model("My rooms");
                m.MyRoomActive = "active";
                m.HaveDropboxConfiguration = ((Context.CurrentUser as User).DropboxApiUserToken != null);
                return View["room/my.html", m];
            };

            Get["/my-rooms"] = _ =>
            {
                int skip = (Request.Query.queries.HasValue ? Request.Query.skip : 0);
                int take = (Request.Query.queries.HasValue ? Request.Query.take : 10);

                var rooms = RavenSession.Query<Room>()
                                            .Where(x => x.User.Id == Context.UserRavenIdString())
                                            .Skip(skip)
                                            .Take(take)
                                            .ToList();

                return rooms;
            };

            Get["/dropbox-access"] = _ =>
            {
                var dropboxCallback = WebConfigurationManager.AppSettings["dropboxCallback"].ToString();
                var token = _dropboxHelper.Client.GetToken();
                var url = _dropboxHelper.Client.BuildAuthorizeUrl(dropboxCallback);

                Session["DropNetUserLoginSecret"] = token.Secret;
                Session["DropNetUserLoginToken"] = token.Token;

                return Response.AsRedirect(url);

            };

            Get["/dropbox-token"] = _ =>
            {
                _dropboxHelper.Client.UserLogin = new UserLogin {
                                                                    Secret = Session["DropNetUserLoginSecret"].ToString(),
                                                                    Token = Session["DropNetUserLoginToken"].ToString()
                                                                };
                var accessToken = _dropboxHelper.Client.GetAccessToken();

                var user = RavenSession.Load<User>(Context.UserRavenIdString());
                user.DropboxApiUserToken = accessToken.Token;
                user.DropboxApiUserSecret = accessToken.Secret;

                RavenSession.Store(user);

                return Response.AsRedirect("/rooms/my");

            };

            Post["/"] = _ =>
            {
                var room = this.Bind<Room>();
                room.LastUpdated = DateTime.UtcNow;
                room.User = Context.CurrentUser as User;
                if (room.Id == null)
                {
                    room.DateCreated = room.LastUpdated;
                }

                var user = Context.CurrentUser as User;
                _dropboxHelper.Client.UserLogin = new DropNet.Models.UserLogin { Token = user.DropboxApiUserToken, Secret = user.DropboxApiUserSecret };
                room.Songs = _dropboxHelper.GetFiles(room.DropboxPath).ToList();

                RavenSession.Store(room);

                return Response.AsJson<Room>(room, HttpStatusCode.Created);
            };

            Put["/"] = _ =>
            {
                this.RequiresAuthentication();

                var room = this.Bind<Room>();
                room.LastUpdated = DateTime.UtcNow;
                if (room.Id == null)
                {
                    room.DateCreated = room.LastUpdated;
                }
                RavenSession.Store(room);

                return Response.AsJson<Room>(room);
            };

            Delete["/"] = _ =>
            {
                this.RequiresAuthentication();

                var room = this.Bind<Room>();
                room = RavenSession.Load<Room>(room.Id);

                if (room == null)
                    return HttpStatusCode.NotFound;

                RavenSession.Delete<Room>(room);

                return Response.AsJson<Room>(room);
            };
        }