示例#1
0
        public static void Handle(Rate temp, Socket socket)
        {
            bool VoteExist = UsersInfos.VoteExist(temp.MusicRatedMID, Program.MyServer.Clients[socket]);

            UsersInfos.AddVoteMusic(temp.MusicRatedMID, Program.MyServer.Clients[socket]);

            if (temp.Type == ElementType.Music)
            {
                var m = Indexation.GetMusicByID(temp.MusicRatedMID);
                if (m != null)
                {
                    if (VoteExist)
                    {
                        m.Rating--;
                    }
                    else
                    {
                        m.Rating++;

                        Program.MyServer.Clients.TryGetValue(socket, out string uid);
                        Program.MyServer.SendObject(new RequestAnswer(UsersInfos.GetLikedMusics(uid)), socket);
                    }
                    Program.MyServer.SendObject(new RateReport(true, temp.MusicRatedMID, m.Rating), socket);
                    MusicsInfo.SaveMusicInfo(m);
                }
            }
            else
            {
                UsersInfos.RatePlaylist(temp.MusicRatedMID, !VoteExist);
                Program.MyServer.SendObject(new RateReport(true, temp.MusicRatedMID, UsersInfos.GetPlaylist(temp.MusicRatedMID).Rating), socket);
            }
        }
示例#2
0
        public static int Do(bool UseMultiThreading)
        {
            string[] ArtistDirs = Directory.GetDirectories(MusicsInfo.GetMusicPath());

            int NumberofMusics = 0;

            TagLib.File file;
            Console.WriteLine();
            ServerMusics.Clear();
            var indexationStopWatch = new Stopwatch();

            indexationStopWatch.Start();
            long startMemory = GC.GetTotalMemory(false);

            foreach (var n in ArtistDirs)
            {
                Author CurrentArtist = new Author(Path.GetFileName(n), n);
                GetArtistImage(n, CurrentArtist);

                string[] AlbumOfArtist = Directory.GetDirectories(n);

                Console.WriteLine(n);
                foreach (var a in AlbumOfArtist)
                {
                    if (!Path.GetFileNameWithoutExtension(a).Contains("-ignore"))
                    {
                        var CurrentAlbum = new Album(CurrentArtist, Path.GetFileName(a), a);
                        var musics       = new ConcurrentBag <Music>();
                        CurrentArtist.Albums.Add(CurrentAlbum);
                        Console.Write($"  {CurrentAlbum.Name} [");

                        if (UseMultiThreading)
                        {
                            Parallel.ForEach(Directory.GetFiles(a), m =>
                            {
                                file = AddMusicToindexation(m, ref NumberofMusics, CurrentArtist, CurrentAlbum, musics);
                            });
                        }
                        else
                        {
                            foreach (var m in Directory.GetFiles(a))
                            {
                                file = AddMusicToindexation(m, ref NumberofMusics, CurrentArtist, CurrentAlbum, musics);
                            }
                        }
                        CurrentAlbum.Musics = musics.OrderBy(x => x.N).ToList();
                        CurrentAlbum.Image  = musics.First().Image;
                        Console.WriteLine("]");
                    }
                }
                ServerMusics.Add(CurrentArtist);
            }
            indexationStopWatch.Stop();
            MyServer.Log.Debug("Indexation finished in " + indexationStopWatch.ElapsedMilliseconds + " Ms, with " + ((GC.GetTotalMemory(false) - startMemory) / 1000000) + " Mo of memory");
            return(NumberofMusics);
        }
示例#3
0
        public static void InitRepository()
        {
            MusicsInfo.SetupMusics();
            string musicsDBPath = MusicsInfo.GetMusicPath();

            if (!Directory.Exists(musicsDBPath))
            {
                Directory.CreateDirectory(musicsDBPath);
            }
            Console.WriteLine("Directory created.");
        }
示例#4
0
        public static void InitRepository()
        {
            string musicsDBPath = @"C:\AllMusics";

            if (!Directory.Exists(musicsDBPath))
            {
                Directory.CreateDirectory(musicsDBPath);
            }
            Console.WriteLine("Directory created.");
            MusicsInfo.SetupMusics();
        }
示例#5
0
        public static bool IsElementExisting(IElement element)
        {
            switch (element.Type)
            {
            case ElementType.Author: return(ServerMusics.Any(a => a.MID == element.MID));

            case ElementType.Album: return(ServerMusics.SelectMany(x => x.Albums).Any(a => a.MID == element.MID));

            case ElementType.Music: return(MusicsInfo.TryFindMusic(element.MID, out XmlNode node));

            case ElementType.Playlist: throw new NotImplementedException();

            default: throw new InvalidOperationException();
            }
        }
示例#6
0
        private static TagLib.File AddMusicToindexation(string m, ref int NumberofMusics, Author CurrentArtist, Album CurrentAlbum, ConcurrentBag <Music> musics)
        {
            TagLib.File file;
            if (Path.GetExtension(m) == ".mp3" || Path.GetExtension(m) == ".flac")
            {
                file = TagLib.File.Create(m);
                string Musicname = file.Tag.Title;
                if (Musicname == null)
                {
                    try
                    {
                        Musicname = Path.GetFileNameWithoutExtension(m).Split('-')[1].Remove(0, 1);
                    }
                    catch
                    {
                        Musicname = Path.GetFileNameWithoutExtension(m);
                    }
                }

                Music current = new Music(Musicname, CurrentArtist, CurrentAlbum, m)
                {
                    Format = Path.GetExtension(m),
                    Genre  = file.Tag.Genres,
                    N      = file.Tag.Track,
                    Image  = file.Tag.Pictures.LastOrDefault()?.Data.ToArray()
                };

                if (current.Genre.Length == 0)
                {
                    current.Genre = new string[] { "Unknown" };
                }
                if (MusicsInfo.TryFindMusic(current.MID, out XmlNode node))
                {
                    var a = MusicsInfo.GetMusicInfo(node);
                    current.Rating = a.Rating;
                    current.Tags   = a.Tags;
                }

                NumberofMusics++;
                musics.Add(current);

                Console.Write(".");
                return(file);
            }
            return(null);
        }
示例#7
0
        public static bool AddElement(UploadMusic tmp)
        {
            if (!Indexation.IsElementExisting(tmp.MusicPart.Musics.First().Author))
            {
                AddAuthor(tmp);
            }

            if (Indexation.IsElementExisting(tmp.MusicPart))
            {
                if (IsElementExisting(tmp.MusicPart.Musics.First()))
                {
                    return(false);
                }
                else
                {
                    if (AddMusic(tmp))
                    {
                        return(true);
                    }
                }
            }
            else
            {
                string path      = AddAlbum(tmp);
                var    music     = tmp.MusicPart.Musics.First();
                string MusicPath = Path.Combine(path, music.Title + music.Format);
                System.IO.File.WriteAllBytes(MusicPath, tmp.MusicPart.Musics.First().FileBinary);
                MusicsInfo.SaveMusicInfo(tmp.MusicPart.Musics.First());
                foreach (var a in ServerMusics)
                {
                    if (a.MID == music.Author.MID)
                    {
                        music.FileBinary = null;
                        music.ServerPath = MusicPath;
                        music.Author     = a;

                        Album tmpAl = new Album(a, tmp.MusicPart.Name, path);
                        music.Album = tmpAl;
                        tmpAl.Add(music);
                        a.Albums.Add(tmpAl);
                        return(true);
                    }
                }
            }
            return(false);
        }
示例#8
0
        public static bool AddMusic(UploadMusic tmp)
        {
            var    music = tmp.MusicPart.Musics.First();
            string path  = Path.Combine(GetElementPath(tmp.MusicPart), music.Title + music.Format);

            System.IO.File.WriteAllBytes(path, tmp.MusicPart.Musics.First().FileBinary);
            MusicsInfo.SaveMusicInfo(tmp.MusicPart.Musics.First());
            var album = GetAlbum(tmp.MusicPart.MID);

            if (album != null)
            {
                music.FileBinary = null;
                music.ServerPath = path;
                music.Author     = album.Author;
                music.Album      = album;
                album.Add(music);
                return(true);
            }
            return(false);
        }
示例#9
0
        public static void ModifyMusic(Element originalElement, string newName, string[] genres)
        {
            Music foundMusic = GetMusic(originalElement);

            if (foundMusic != null)
            {
                foundMusic.Title = newName;
                foundMusic.MID   = Utility.Hash.SHA256Hash(foundMusic.Title + foundMusic.Author.Name);

                TagLib.File file = TagLib.File.Create(foundMusic.ServerPath);
                file.Tag.Title = foundMusic.Title;

                if (genres != null)
                {
                    foundMusic.Genre = genres;
                    file.Tag.Genres  = genres;
                }

                file.Save();

                MusicsInfo.EditMusicsInfo(originalElement.MID, foundMusic);
            }
        }
示例#10
0
 public static void SaveAllInfos()
 {
     MusicsInfo.SaveMusicsInfo(GetAllMusics());
 }