public SimpleFileAbstraction(SimpleFile file)
 {
     this.file = file;
 }
        public Mp3RecordManager(IActorRef resourceDownloader, IActorRef resourceStorer)
        {
            Receive<NewRecordMessage>(async message =>
            {
                newRecord = message;
                path = message.Artist + "\\" + message.Album + "\\" + message.Track + ".mp3";
                var pathHash = MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(path));
                var rowKey = message.Album + " - " + message.Track; //BitConverter.ToString(pathHash).Replace("-", "");
                var tableClient = storageAccount.CreateCloudTableClient();
                var table = tableClient.GetTableReference(StorageTableName);
                trackEntity = new TrackEntity
                {
                    Album = message.Album,
                    AlbumArtUrl = message.AlbumImageLocation.AbsoluteUri,
                    AlbumArtDownloaded = false,
                    Artist = message.Artist,
                    Track = message.Track,
                    TrackDownloaded = false,
                    TrackUrl = message.FileLocation.AbsoluteUri,
                    PartitionKey = message.Artist,
                    RowKey = rowKey,
                    Timestamp = DateTime.UtcNow
                };
                var partitionFilter = TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal,
                    trackEntity.PartitionKey);
                var rowFilter = TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.Equal, trackEntity.RowKey);
                var finalFilter = TableQuery.CombineFilters(partitionFilter, TableOperators.And, rowFilter);
                var query = new TableQuery<TrackEntity>().Where(finalFilter);

                var entities = table.ExecuteQuery(query, new TableRequestOptions {RetryPolicy = new NoRetry()});
                if (!entities.Any())
                {
                    var insertOperation = TableOperation.Insert(trackEntity);
                    table.Execute(insertOperation);

                    if (message.AlbumImageLocation != null)
                    {
                        var albumArtDownloadedMessage =
                            await
                                resourceDownloader.Ask<AlbumArtDownloaded>(
                                    new DownloadAlbumArt(message.AlbumImageLocation));
                        trackEntity.AlbumImage = albumArtDownloadedMessage.Resource;
                    }
                    var mp3Downloaded =
                        await resourceDownloader.Ask<Mp3Downloaded>(new DownloadMp3(message.FileLocation));

                    Log.Information("Received downloaded MP3. Length: {length}, Location: {resourceUri}", mp3Downloaded.Resource.Length,
                        mp3Downloaded.ResourceUri);

                    var memoryStream = new MemoryStream();
                    memoryStream.Write(mp3Downloaded.Resource, 0, mp3Downloaded.Resource.Length);

                    var simpleFile = new SimpleFile(path, memoryStream);
                    var simpleFileAbstraction = new SimpleFileAbstraction(simpleFile);
                    var file = File.Create(simpleFileAbstraction);

                    file.Tag.Composers = new[] {newRecord.Artist};
                    file.Tag.AlbumArtists = new[] {newRecord.Artist};
                    file.Tag.Title = newRecord.Track;
                    file.Tag.Album = newRecord.Album;

                    file.Tag.Pictures = new IPicture[]
                    {
                        new Picture(trackEntity.AlbumImage)
                    };

                    file.Save();
                    var savedFile = ReadToEnd(simpleFile.Stream);
                    resourceStorer.Tell(new StoreBlobRequest(path, savedFile));

                    Log.Information("Creating record: {artist}", message.Artist);
                }
            });

            Receive<Mp3Downloaded>(message =>
            {
                Log.Information("receieved downloaded MP3. Length: {length}, Resource URI: {resourceUri}",
                    message.Resource.Length,
                    message.ResourceUri);

                var memoryStream = new MemoryStream();
                memoryStream.Write(message.Resource, 0, message.Resource.Length);

                var simpleFile = new SimpleFile(path, memoryStream);
                var simpleFileAbstraction = new SimpleFileAbstraction(simpleFile);
                var file = File.Create(simpleFileAbstraction);

                file.Tag.Composers = new[] {newRecord.Artist};
                file.Tag.AlbumArtists = new[] {newRecord.Artist};
                file.Tag.Title = newRecord.Track;
                file.Tag.Album = newRecord.Album;

                file.Tag.Pictures = new IPicture[]
                {
                    new Picture(trackEntity.AlbumImage)
                };

                file.Save();
                var savedFile = ReadToEnd(simpleFile.Stream);
                resourceStorer.Tell(new StoreBlobRequest(path, savedFile));
            });

            Receive<AlbumArtDownloaded>(message =>
            {
                // this is the Album Art Image file.
                if (newRecord.AlbumImageLocation != null &&
                    message.ResourceUri.AbsoluteUri == newRecord.AlbumImageLocation.AbsoluteUri)
                {
                    var path = newRecord.Artist + "\\" + newRecord.Album + ".jpg";
                    resourceStorer.Tell(new StoreBlobRequest(path, message.Resource));
                }
            });
        }
 public SimpleFileAbstraction(SimpleFile file)
 {
     this.file = file;
 }