Example #1
0
 public Track Get(int id)
 {
     return(_multiSourcePlaylistRepository.GetTrack(id));
 }
        public async Task <FileStreamResult> Get(int id)
        {
            var claimsIdentity = User.Identity as ClaimsIdentity;
            var userId         = Convert.ToInt64(claimsIdentity.Claims.FirstOrDefault(claim => claim.Type == "Id").Value);
            var user           = _multiSourcePlaylistRepository.GetUser(userId);
            var track          = _multiSourcePlaylistRepository.GetTrack(id);

            byte[] audioArray = new byte[0];

            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
                _configuration["Production:StorageConnectionString"]);

            CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
            // Get a reference to the file share we created previously.
            CloudFileShare     share   = fileClient.GetShareReference(user.FileFolder);
            CloudFileDirectory userDir = null;

            // Ensure that the share exists.
            if (await share.ExistsAsync())
            {
                // Get a reference to the root directory for the share.
                CloudFileDirectory rootDir = share.GetRootDirectoryReference();
                // Get a reference to the directory we created previously.
                userDir = rootDir.GetDirectoryReference("audio");
                // Ensure that the directory exists.
                if (await userDir.ExistsAsync())
                {
                    var audiofile = userDir.GetFileReference(track.Address);
                    if (await audiofile.ExistsAsync())
                    {
                        await audiofile.FetchAttributesAsync();

                        audioArray = new byte[audiofile.Properties.Length];
                        await audiofile.DownloadToByteArrayAsync(audioArray, 0);
                    }
                }
                else
                {
                    return(null);
                }
            }
            else
            {
                return(null);
            }
            long fSize        = audioArray.Length;
            long startbyte    = 0;
            long endbyte      = fSize - 1;
            int  statusCode   = 200;
            var  rangeRequest = Request.Headers["Range"].ToString();

            if (rangeRequest != "")
            {
                string[] range = Request.Headers["Range"].ToString().Split(new char[] { '=', '-' });
                startbyte = Convert.ToInt64(range[1]);
                if (range.Length > 2 && range[2] != "")
                {
                    endbyte = Convert.ToInt64(range[2]);
                }
                if (startbyte != 0 || endbyte != fSize - 1 || range.Length > 2 && range[2] == "")
                {
                    statusCode = 206;
                }
            }

            long desSize = endbyte - startbyte + 1;

            Response.StatusCode  = statusCode;
            Response.ContentType = "audio/mp3";
            Response.Headers.Add("Content-Accept", Response.ContentType);
            Response.Headers.Add("Content-Length", desSize.ToString());
            Response.Headers.Add("Content-Range", string.Format("bytes {0}-{1}/{2}", startbyte, endbyte, fSize));
            Response.Headers.Add("Accept-Ranges", "bytes");
            Response.Headers.Remove("Cache-Control");
            var stream = new MemoryStream(audioArray, (int)startbyte, (int)desSize);

            return(new FileStreamResult(stream, "audio/mp3")
            {
                FileDownloadName = track.Name
            });
        }