Beispiel #1
0
        public async Task AfterCallingCreateAndUpload_UploadedFileShouldBeTheSameAsTheOriginalFile(FileInfo file, bool passAsFileInfo)
        {
            var sut = new TusClient();

            string url;

            if (passAsFileInfo)
            {
                url = await sut.CreateAsync(TusEndpoint, file);
            }
            else
            {
                url = await sut.CreateAsync(TusEndpoint, file.Length);
            }

            await sut.UploadAsync(url, file);

            var upload = new FileInfo(Path.Combine(_dataDirectoryPath, $"{url.Split('/').Last()}.bin"));

            upload.Exists.ShouldBe(true);
            upload.Length.ShouldBe(file.Length);
            using (var fileStream = new FileStream(file.FullName, FileMode.Open, FileAccess.Read))
                using (var uploadStream = new FileStream(upload.FullName, FileMode.Open, FileAccess.Read))
                {
                    var fileBytes = new byte[fileStream.Length];
                    fileStream.Read(fileBytes, 0, fileBytes.Length);
                    var uploadBytes = new byte[uploadStream.Length];
                    uploadStream.Read(uploadBytes, 0, uploadBytes.Length);
                    SHA1(uploadBytes).ShouldBe(SHA1(fileBytes));
                }
        }
        /// <summary>
        /// Upload a file to the Tus server.
        /// </summary>
        /// <param name="url">URL to a previously created file.</param>
        /// <param name="fileStream">A file stream of the file to upload. The stream will be closed automatically.</param>
        /// <param name="chunkSize">The size, in megabytes, of each file chunk when uploading.</param>
        /// <param name="cancellationToken">A cancellation token to cancel the operation with.</param>
        /// <returns>A <see cref="UploadStatus"/> which represents the upload operation.</returns>
        public static async Task <UploadStatus> UploadAsync(
            string url,
            Stream fileStream,
            double chunkSize = 5.0,
            CancellationToken cancellationToken = default)
        {
            var result = new UploadStatus();

            try
            {
                var tusClient = new TusClient();
                var fileUrl   = await tusClient.CreateAsync(url, fileStream.Length);

                var   uploadOperation = tusClient.UploadAsync(fileUrl, fileStream, chunkSize, cancellationToken);
                await uploadOperation;
                result.status = "Upload success";
                result.url    = StringParser.ConvertSourceToTus(fileUrl);
                return(result);
            }
            catch (Exception e)
            {
                result.errors.Add(e.ToString());
                result.status = "Upload error";
                return(result);
            }
        }
Beispiel #3
0
        public async Task Upload(IFormFile file)
        {
            var filePath = SaveAndGetFilePath(file);

            var _file   = new FileInfo(@filePath);
            var client  = new TusClient();
            var fileUrl = await client.CreateAsync("http://localhost:3000/upload", _file.Length);

            var uploadOperation = client.UploadAsync(fileUrl, _file, 5D);

            uploadOperation.Progressed += (transferred, total) =>
                                          Console.WriteLine($"{((float)transferred / (float)total) * 100.00}%");

            await uploadOperation;
        }
        public async Task CallingHead_ShouldReturnProgressOfUploadedFile(FileInfo file)
        {
            var sut = new TusClient();

            var url = await sut.CreateAsync(TusEndpoint, file.Length);

            var headBeforeUpload = await sut.HeadAsync(url);

            await sut.UploadAsync(url, file);

            var headAfterUpload = await sut.HeadAsync(url);

            headBeforeUpload.Headers.Keys.ShouldContain("Upload-Offset");
            headBeforeUpload.Headers["Upload-Offset"].ShouldBe("0");
            headAfterUpload.Headers.Keys.ShouldContain("Upload-Offset");
            headAfterUpload.Headers["Upload-Offset"].ShouldBe(file.Length.ToString());
        }
        public async Task AfterCallingDownload_DownloadedFileShouldBeTheSameAsTheOriginalFile(FileInfo file)
        {
            var sut = new TusClient();

            var url = await sut.CreateAsync(TusEndpoint, file.Length);

            await sut.UploadAsync(url, file);

            var response = await sut.DownloadAsync(url);

            using (var fileStream = new FileStream(file.FullName, FileMode.Open, FileAccess.Read))
            {
                var fileBytes = new byte[fileStream.Length];
                fileStream.Read(fileBytes, 0, fileBytes.Length);
                SHA1(response.ResponseBytes).ShouldBe(SHA1(fileBytes));
            }
        }
        public async Task CallingDelete_ShouldRemoveUploadedFile(FileInfo file)
        {
            var sut = new TusClient();


            var url = await sut.CreateAsync(TusEndpoint, file.Length);

            await sut.UploadAsync(url, file);

            var uploadHeadResponse = await sut.HeadAsync(url);

            var deleteResult = await sut.Delete(url);


            deleteResult.ShouldBe(true);
            uploadHeadResponse.Headers.Keys.ShouldContain("Upload-Offset");
            uploadHeadResponse.Headers["Upload-Offset"].ShouldBe(file.Length.ToString());
            File.Exists(Path.Combine(_dataDirectoryPath, $"url.Split('/').Last()")).ShouldBe(false);
        }
Beispiel #7
0
    public async void StartUpload(Queue <FileUpload> filesToUpload)
    {
        Assert.IsNull(status, "status should be null");

        status = new UploadStatus();
        status.filesUploaded = new List <FileUpload>();
        status.filesToUpload = filesToUpload;
        status.projectId     = filesToUpload.Peek().projectGuid;
        UpdateStatusBySavedProgress(status);

        status.totalSizeBytes = ProjectSizeFromStatus(status);
        status.uploadedBytes  = BytesUploadedFromStatus(status);

        var client = new TusClient();

        while (status.filesToUpload.Count != 0)
        {
            if (status.failed)
            {
                return;
            }

            bool shouldResume = false;
            if (status.fileInProgress == null)
            {
                status.fileInProgress = status.filesToUpload.Dequeue();
            }
            else
            {
                shouldResume = true;
            }

            var headers = client.AdditionalHeaders;
            headers.Clear();
            headers.Add("guid", status.fileInProgress.projectGuid.ToString());
            headers.Add("type", status.fileInProgress.type.ToString());
            headers.Add("Cookie", Web.formattedCookieHeader);
            headers.Add("filename", status.fileInProgress.filename);

            var filesize = FileHelpers.FileSize(status.fileInProgress.path);

            //NOTE(Simon): If we're not resuming, get a new fileId from the server
            if (!shouldResume)
            {
                (int code, string message)createResult;
                try
                {
                    createResult = await client.CreateAsync(Web.fileUrl, filesize);
                }
                catch (Exception e)
                {
                    FailUpload("Something went wrong while trying to upload this project. Please try again later", e);
                    return;
                }

                if (createResult.code != 200)
                {
                    status.filesToUpload.Enqueue(status.fileInProgress);
                    status.fileInProgress = null;

                    FailUpload($"HTTP error {createResult.code}: {createResult.message}");
                    return;
                }

                status.fileInProgressId = createResult.message;
            }

            try
            {
                var uploadOp = client.UploadAsync(status.fileInProgressId, File.OpenRead(status.fileInProgress.path), 20);
                uploadOp.Progressed += OnUploadProgress;
                var _ = await uploadOp;
            }
            catch (Exception e)
            {
                FailUpload("Something went wrong while trying to upload this project. Please try again later", e);
                return;
            }

            status.filesUploaded.Add(status.fileInProgress);
            status.fileInProgress = null;

            status.uploadedBytes           += (ulong)filesize;
            status.currentFileProgressBytes = 0;

            //NOTE(Simon): After finishing a file, write progress to disk
            WriteUploadProgress(status);
        }

        ClearUploadProgress();
        if (FinishUpload())
        {
            SucceedUpload();
        }
        else
        {
            FailUpload("Something went wrong while finishing the upload. Please try again later");
        }
    }
Beispiel #8
0
        /*
         * This example upload a local file,
         * transcode it and then,
         * download the transcoded file
         */

        static void Main(string[] args)
        {
            // You can find your API key under Project settings in your Dashboard on Qencode portal
            const string apiKey = "YOUR_API_KEY_HERE";
            const string relative_output_dir = "TranscodedOutput";             // relative output dir

            // This is the full file name of the source video
            string sourceVideoFullFileName = "E:\\dev\\My\\Sample720.flv";

            // if an argument is specified, the source video file will be readed from the first argument
            if (args.Length >= 1 && !string.IsNullOrEmpty(args[0]))
            {
                sourceVideoFullFileName = args[0];
            }

            try
            {
                // get access token
                Console.WriteLine("Requesting access token...");
                var q = new QencodeApiClient(apiKey);
                Console.WriteLine("\taccess token: '" + q.AccessToken + "'");

                // create a new task
                Console.WriteLine("Creating a new task...");
                var task = q.CreateTask();
                Console.WriteLine("\tcreated new task with token: '" + task.TaskToken + "' and url for direct video upload (TUS) '" + task.UploadUrl + "'");

                // direct video upload - initiate upload (get endpoint for task)
                Console.WriteLine("Initiate upload...");
                var srcFI  = new FileInfo(sourceVideoFullFileName);
                var client = new TusClient();
                var tusUploadLocationUrl = client.CreateAsync(task.UploadUrl + "/" + task.TaskToken, srcFI.Length).Result;
                Console.WriteLine("\tobtained TUS upload location: '" + tusUploadLocationUrl + "'");

                // direct video upload - send data
                var uploadOperation = client.UploadAsync(tusUploadLocationUrl, srcFI);
                Console.WriteLine("\ttransfer started");
                uploadOperation.Progressed += (transferred, total) =>
                {
                    Console.CursorLeft = 0;
                    Console.Write($"Progress: {transferred}/{total}");
                };
                Console.WriteLine();
                Console.WriteLine();
                uploadOperation.GetAwaiter().GetResult();
                Console.WriteLine("\tupload done");

                // define a custom task by reading query.json and filling the ##TUS_FILE_UUID## placeholder
                var tusFileUUID             = tusUploadLocationUrl.Substring(tusUploadLocationUrl.LastIndexOf('/') + 1);
                var customTrascodingJSON    = File.ReadAllText("query.json").Replace("##TUS_FILE_UUID##", tusFileUUID);
                var customTranscodingParams = CustomTranscodingParams.FromJSON(customTrascodingJSON);

                // start a custom task
                Console.WriteLine("Custom task starting..");
                Console.WriteLine(customTrascodingJSON);

                // start a custom task - set event handler
                bool taskCompletedOrError = false;
                task.TaskCompleted = new RunWorkerCompletedEventHandler(
                    delegate(object o, RunWorkerCompletedEventArgs e)
                {
                    if (e.Error != null)
                    {
                        taskCompletedOrError = true;
                        Console.WriteLine("Error: ", e.Error.Message);
                    }

                    var response = e.Result as TranscodingTaskStatus;
                    if (response.error == 1)
                    {
                        taskCompletedOrError = true;
                        Console.WriteLine("Error: " + response.error_description);
                    }
                    else if (response.status == "completed")
                    {
                        taskCompletedOrError = true;
                        Console.WriteLine("Video urls: ");
                        foreach (var video in response.videos)
                        {
                            Console.WriteLine(video.url);
                        }
                    }
                    else
                    {
                        Console.WriteLine(response.status);
                    }
                });
                // start a custom task - start
                Console.WriteLine("\tstarting...");
                var started = task.StartCustom(customTranscodingParams);                 // starts and poll

                // waiting
                Console.WriteLine("Waiting..");
                while (!taskCompletedOrError)
                {
                    Thread.Sleep(1000);
                }

                // get download url
                if (task.LastStatus == null)
                {
                    throw new InvalidOperationException("Unable to obtain download url");
                }
                var outputDownloadUrl = new Uri(task.LastStatus.videos.First().url);
                Console.WriteLine("Output download url: '" + outputDownloadUrl.ToString() + "'");
                string output_file_name = GetOutputFileName(sourceVideoFullFileName, outputDownloadUrl.Segments.Last());
                Console.WriteLine("Output file name: '" + output_file_name + "'");


                // download
                Console.WriteLine("Downloading..");
                HttpFileDownload(outputDownloadUrl, relative_output_dir, output_file_name);
                Console.WriteLine("\tdownload done");
                Environment.Exit(0);
            }
            catch (QencodeApiException e)
            {
                Console.WriteLine("Qencode API exception: " + e.Message);
                Environment.Exit(-1);
            }
            catch (QencodeException e)
            {
                Console.WriteLine("API call failed: " + e.Message);
                Environment.Exit(-1);
            }
        }