Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
jxpxxzj committed Oct 3, 2019
1 parent f2bbc43 commit fba809c
Show file tree
Hide file tree
Showing 17 changed files with 379 additions and 112 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -434,4 +434,5 @@ $RECYCLE.BIN/

*.litedb

.vscode/
.vscode/
music/
21 changes: 21 additions & 0 deletions PrivateCloudMusic/Entities/Music.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.IO;
using LiteDB;
using Pcm.Proto;

namespace Pcm.Entities
{
Expand All @@ -15,11 +16,31 @@ public class Music

public uint Track { get; set; }
public uint TrackCount { get; set; }
public int PictureCount { get; set; }

public string FileName => Path.GetFileName(FilePath);
public string FilePath { get; set; }
public int PlayCount { get; set; }
public string MimeType { get; set; }
public long Length { get; set; }

public GetMusicResponseBody ToResp() => new GetMusicResponseBody()
{
Id = MusicId.ToString(),
Title = Title,
Album = Album ?? string.Empty,
Genres = { Genres },
Performers = { Performers },
Track = Track,
TrackCount = TrackCount != 0 ? TrackCount : Track,
PictureCount = PictureCount,
FileName = FileName,
PlayCount = PlayCount,
Length = Length,
MimeType = MimeType,
CreatedAt = MusicId.Timestamp
};

public static implicit operator GetMusicResponseBody(Music music) => music.ToResp();
}
}
2 changes: 0 additions & 2 deletions PrivateCloudMusic/Entities/Playlist.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,5 @@ public class Playlist

[BsonRef("music")]
public List<Music> Musics { get; set; }

public DateTime CreatedAt { get; set; }
}
}
85 changes: 78 additions & 7 deletions PrivateCloudMusic/Handlers/CategoryHandler.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,101 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Pcm.Entities;
using Pcm.Proto;
using Pcm.Proxy;
using Pcm.Services;
using Pcm.Utils;
using System;

namespace Pcm.Handlers
{
public class CategoryHandler
{
public async Task<StatusCode> Get(Context ctx, Request req, Response resp)
{
return await Task.FromResult(StatusCode.Ok);
}

public async Task<StatusCode> List(Context ctx, Request req, Response resp)
public async Task<StatusCode> ListByAlbum(Context ctx, Request req, Response resp)
{
var musics = MusicService.Instance.Collection.FindAll()
.Select(t => t.ToResp())
.GroupBy(m => m.Album)
.ToDictionary(m => m.Key, m => new ListMusicResponseBody() { Musics = { m.ToList() }});

resp.Body.ListByAlbum = new ListByAlbumResponseBody()
resp.Body.ListByAlbumBody = new ListByAlbumResponseBody()
{
Albums = { musics }
};
return await Task.FromResult(StatusCode.Ok);
}


public async Task<StatusCode> ListByPerformer(Context ctx, Request req, Response resp)
{
var musics = MusicService.Instance.Collection.FindAll()
// extend to musics with single performer
.SelectMany(m => m.Performers.Select(performer => new Music()
{
MusicId = m.MusicId,
Title = m.Title,
Album = m.Album,
Genres = m.Genres,
Performers = new[] {performer},
Track = m.Track,
TrackCount = m.TrackCount,
PictureCount = m.PictureCount,
FilePath = m.FilePath,
PlayCount = m.PlayCount,
Length = m.Length,
MimeType = m.MimeType
}))
.GroupBy(t => t.Performers[0])
.ToDictionary(t => t.Key, t => new List<GetMusicResponseBody>(t.Select(m => m.MusicId)
.Distinct()
.Select(m => MusicService.Instance.Get(m.ToString()))
.Select(m => m.ToResp())))
.ToDictionary(t => t.Key, t => new ListMusicResponseBody() { Musics = { t.Value }});

resp.Body.ListByPerformerBody = new ListByPerformerResponseBody()
{
Performers = { musics }
};
return await Task.FromResult(StatusCode.Ok);
}

public async Task<StatusCode> Search(Context ctx, Request req, Response resp)
{
var keywords = req.Body.ListSearchBody.Keyword.ToLower().RemoveSpecialCharacters().Split(" ");
var searchThreshold = ConfigManager.GetInt("searchThreshold", 2);

var musics = MusicService.Instance.Collection.FindAll()
// extend to musics with single performer
.SelectMany(m => m.Performers.Select(performer => new Music()
{
MusicId = m.MusicId,
Title = m.Title,
Album = m.Album ?? string.Empty,
Genres = m.Genres,
Performers = new[] {performer},
Track = m.Track,
TrackCount = m.TrackCount,
PictureCount = m.PictureCount,
FilePath = m.FilePath,
PlayCount = m.PlayCount,
Length = m.Length,
MimeType = m.MimeType
}))
.Where(t => t.Album.ToLower().Distance(keywords) <= searchThreshold ||
t.Title.ToLower().Distance(keywords) <= searchThreshold ||
t.Performers[0].ToLower().Distance(keywords) <= searchThreshold ||
t.FileName.ToLower().Distance(keywords) <= searchThreshold)
.Select(t => t.MusicId)
.Distinct()
.Select(t => MusicService.Instance.Get(t.ToString()))
.Select(t => t.ToResp());

resp.Body.ListSearchBody = new ListSearchResponseBody()
{
Musics = { musics }
};
return await Task.FromResult(StatusCode.Ok);
}
}
}
24 changes: 9 additions & 15 deletions PrivateCloudMusic/Handlers/MusicHandler.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Linq;
using System.Threading.Tasks;
using Pcm.Proto;
using Pcm.Proxy;
Expand All @@ -12,27 +13,20 @@ public async Task<StatusCode> Get(Context ctx, Request req, Response resp)
var music = MusicService.Instance.Get(req.Body.GetMusicBody.Id);
if (music != null)
{
resp.Body.GetMusicBody = new GetMusicResponseBody()
{
Title = music.Title,
Album = music.Album,
Genres = { music.Genres },
Performers = { music.Performers },
Track = music.Track,
TrackCount = music.TrackCount,
FileName = music.FileName,
PlayCount = music.PlayCount,
Length = music.Length,
MimeType = music.MimeType,
CreatedAt = music.MusicId.Timestamp
};
resp.Body.GetMusicBody = music;
}

return await Task.FromResult(StatusCode.Ok);
}

public async Task<StatusCode> List(Context ctx, Request req, Response resp)
{
var musics = MusicService.Instance.Collection.FindAll();
resp.Body.ListMusicBody = new ListMusicResponseBody()
{
Musics = { musics.Select(m => m.ToResp()) }
};

return await Task.FromResult(StatusCode.Ok);
}

Expand Down
13 changes: 13 additions & 0 deletions PrivateCloudMusic/PrivateCloudMusic.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@
<ItemGroup>
<None Remove="Protos\api.proto" />
<Protobuf Include="Proto\api.proto" />
<None Remove="music\**" />
</ItemGroup>

<ItemGroup>
<Compile Remove="music\**" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Remove="music\**" />
</ItemGroup>

<ItemGroup>
<Content Remove="music\**" />
</ItemGroup>

</Project>
8 changes: 1 addition & 7 deletions PrivateCloudMusic/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Pcm.Services;
using Pcm.Utils;

namespace Pcm
Expand All @@ -11,15 +10,10 @@ public class Program
public static void Main(string[] args)
{
ConfigManager.Config = new ConfigurationBuilder()
.AddJsonFile("config.json", optional: true) //this is not needed, but could be useful
.AddJsonFile("config.json", true) //this is not needed, but could be useful
.AddEnvironmentVariables()
.AddCommandLine(args)
.Build();

var m = MusicService.Instance.Add(@"/Users/xiaozijin/Downloads/03.オードリー.mp3");



CreateHostBuilder(args).Build().Run();
}

Expand Down
Loading

0 comments on commit fba809c

Please sign in to comment.