コード例 #1
0
        /// <summary>
        /// Requests a download token for a given resource.
        /// </summary>
        /// <param name="resourceIdentifier"></param>
        /// <param name="includeFileHash">Whether a file hash for the
        /// requested resource should be calculated and assigned to the
        /// <see cref="DownloadToken.Md5FileHash"/> property of the returned
        /// <see cref="DownloadToken"/>.</param>
        /// <returns></returns>
        public DownloadToken RequestDownloadToken(string resourceIdentifier, bool includeFileHash)
        {
            FileInfo fileInfo = FileResolveFunc(resourceIdentifier);

            if (!fileInfo.Exists)
            {
                string msg = String.Format("Resource [{0}] not found.", resourceIdentifier);
                throw new VirtualResourceNotFoundException(msg);
            }

            string transferId = Guid.NewGuid().ToString();

            DownloadToken dt = new DownloadToken
            {
                TransferId         = transferId,
                ResourceIdentifier = resourceIdentifier,
                CreationTime       = SystemTime.Now(),
                ContentType        = ContentUtil.ResolveContentType(fileInfo.Extension),
                DownloadBlockSize  = 512 * 1024,           //TODO configure block size and expiration
                ResourceName       = fileInfo.Name,
                ResourceLength     = fileInfo.Length,
                ExpirationTime     = SystemTime.Now().AddHours(24),
                Status             = TransferStatus.Starting
            };

            //calculate number of blocks
            long count = dt.ResourceLength / dt.DownloadBlockSize.Value;

            if (dt.ResourceLength % dt.DownloadBlockSize != null)
            {
                count++;
            }
            dt.TotalBlockCount = count;

            if (includeFileHash)
            {
                dt.Md5FileHash = fileInfo.CalculateMd5Hash();
            }

            var transfer = new DownloadTransfer(dt)
            {
                File = fileInfo
            };

            Transfers.Add(transferId, transfer);
            return(dt);
        }