Exemplo n.º 1
0
        private async Task <ObjectModel> UploadAsync(string dataPath, CancellationToken ct)
        {
            string uid      = Guid.NewGuid().ToString();
            string filename = Path.GetFileName(dataPath);

            using (FileStream stream = new FileStream(dataPath, FileMode.Open))
            {
                long chunkLength = 1048576;
                if (stream.Length < chunkLength)
                {
                    chunkLength = stream.Length;
                }
                long totalChunks = stream.Length / chunkLength;

                byte[] chunk       = new byte[chunkLength];
                int    chunkNumber = 1;
                int    maximumNumberOfBytesToRead = chunk.Length;
                while (stream.Read(chunk, 0, maximumNumberOfBytesToRead) > 0)
                {
                    using (MemoryStream chunkStream = new MemoryStream(chunk))
                    {
                        await uploader.UploadAsync(Convert.ToInt32(totalChunks), chunkNumber, uid, chunkStream, ct);
                    }

                    long numberOfBytesToReadLeft = stream.Length - stream.Position;
                    if (numberOfBytesToReadLeft < maximumNumberOfBytesToRead)
                    {
                        maximumNumberOfBytesToRead = (int)numberOfBytesToReadLeft;
                        chunk = new byte[maximumNumberOfBytesToRead];
                    }
                    chunkNumber++;
                }
            }
            return(await uploader.CommitAsync(filename, filename, uid, ct));
        }
        public async Task <IActionResult> UploadAsync(
            IFormFile file,
            int chunkNumber,
            int totalSize,
            string uid,
            string filename,
            string relativePath,
            int totalChunks)
        {
            try
            {
                if (!Guid.TryParse(uid, out Guid parsedUid))
                {
                    throw new ArgumentException("The provided unique identifier is not valid.");
                }

                ThreadThrottler throttler = new ThreadThrottler();

                CancellationTokenSource cts = CancellationTokenSource.CreateLinkedTokenSource(
                    HttpContext?.RequestAborted ?? default(CancellationToken));

                var chunkResult = await uploader.UploadAsync(totalChunks, chunkNumber, uid, file.OpenReadStream(), cts.Token);

                if (ApiConfiguration?.Options?.EnableRateLimiting ?? false)
                {
                    // Ensure current request lasts at least 500 milliseconds
                    throttler.Throttle(500);
                }

                if (chunkResult != null && chunkResult.IsCompleted)
                {
                    var result = await uploader.CommitAsync(filename, relativePath, uid, cts.Token);

                    return(CreatedAtAction(nameof(GetById), new { id = result.Id }, result));
                }
                else
                {
                    return(Content(string.Empty));
                }
            }
            catch (ArgumentException e)
            {
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(Content(e.Message));
            }
            catch (Exception e)
            {
                Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                return(Content(e.Message));
            }
        }
        public async Task <IActionResult> UploadAsync(
            IFormFile file,
            int chunkNumber,
            int totalSize,
            string uid,
            string filename,
            string relativePath,
            int totalChunks)
        {
            try
            {
                if (!Guid.TryParse(uid, out Guid parsedUid))
                {
                    throw new ArgumentException("The provided unique identifier is not valid.");
                }

                var chunkResult = await uploader.UploadAsync(totalChunks, chunkNumber, uid, file.OpenReadStream(), CancellationToken);

                if (chunkResult != null && chunkResult.IsCompleted)
                {
                    var result = await uploader.CommitAsync(filename, relativePath, uid, CancellationToken);

                    return(CreatedAtAction(nameof(GetById), new { id = result.Id }, result));
                }
                else
                {
                    return(Content(string.Empty));
                }
            }
            catch (ArgumentException e)
            {
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(Content(e.Message));
            }
            catch (Exception e)
            {
                Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                return(Content(e.Message));
            }
        }