Code Monkey home page Code Monkey logo

go-tus's Introduction

go-tus Build Status Go Report Card GoDoc

A pure Go client for the tus resumable upload protocol

Example

package main

import (
    "os"
    "github.com/eventials/go-tus"
)

func main() {
    f, err := os.Open("my-file.txt")

    if err != nil {
        panic(err)
    }

    defer f.Close()

    // create the tus client.
    client, _ := tus.NewClient("https://tus.example.org/files", nil)

    // create an upload from a file.
    upload, _ := tus.NewUploadFromFile(f)

    // create the uploader.
    uploader, _ := client.CreateUpload(upload)

    // start the uploading process.
    uploader.Upload()
}

Features

This is not a full protocol client implementation.

Checksum, Termination and Concatenation extensions are not implemented yet.

This client allows to resume an upload if a Store is used.

Built in Store

Store is used to map an upload's fingerprint with the corresponding upload URL.

Name Backend Dependencies
MemoryStore In-Memory None
LeveldbStore LevelDB goleveldb

Future Work

  • SQLite store
  • Redis store
  • Memcached store
  • Checksum extension
  • Termination extension
  • Concatenation extension

go-tus's People

Contributors

alexandrevicenzi avatar andrewmostello avatar breno-alves avatar hmalphettes avatar josemauro avatar macewinduu avatar oliverpool avatar pkochubey avatar skrater avatar thiagoflima avatar vivianliang avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

go-tus's Issues

Inspection of upload HTTP requests

Hello developers, I'm facing an issue using this library. While not being a direct issue of this library, the way this library uses the http.Client prevent me from doing more deep inspection of the underline calls.

I'm trying to perform an upload to Cloudflare Stream (video hosting CDN). I'm sending a malformed request, as the upload endpoint returns 400. This is my fault and I would like to debug it.

The code I'm using is this (extracted for clarity):

func (c UploadClient) Upload() {
	config := tus.DefaultConfig()
	config.ChunkSize = 5 * 1024 * 1024 // Cloudflare Stream requires a minimum chunk size of 5MB.
	config.Store = inMemoryStore
	config.Resume = true
	config.Header.Add("X-Auth-Email", cloudflareEmail)
	config.Header.Add("X-Auth-Key", cloudflareAPIKey)

	apiURL := "https://api.cloudflare.com/client/v4/accounts/" + accountID + "/media"
	// err will always be nil with these parameters
	client, _ := tus.NewClient(apiURL, config)
	upload, err := tus.NewUploadFromFile(file)
	if err != nil {
		return "", errors.WithMessage(err, "cannot create upload from file")
	}
    [...]
	uploader, err := client.CreateUpload(upload)
	if err != nil {
		return "", errors.WithMessage(err, "cannot create upload")
	}
    // [...]
}

Failure happens of client.CreateUpload(upload), the err returned is cannot create upload: unexpected status code: 400

I suspect responsible is this snippet

go-tus/client.go

Lines 86 to 88 in 9db4742

if err != nil {
return nil, err
}

I would like to be able to inspect the HTTP response, so far I tried:

Both required modification to the source code of this library, as I wasn't able to find a way to use a different http.Client in the tus client.

Any suggestion? I'm willing to provide changes to accommodate a custom http.Client if interested.

Thank you!

Connection timeout too long

Hi,

I implemented a tus client with resumable upload but I am now facing a problem with timeouts. When I move down the interface on my machine or plug out the ethernet cable while a new upload gets created (client.CreateOrResumeUpload(upload)), it takes about 6 minutes to throw an error. Even though the internet connection is back again.

%!(EXTRA *url.Error=Post <ip>/files/: dial tcp <ip>: connect: network is unreachable)"

Is there a way to decrease timeouts or something else?

Uploading larger files

Hello,

I recommend using your library to implement the tus protocol for go-vimeo. One of our users encountered a problem when working with large files.

Perhaps the problem does exist: silentsokolov/go-vimeo#10

Thx

'Location' expects absolute URL

So I've been doing some testing using tusc, which is a statically compiled binary using this library. Everything worked great with it when using tusd as the server. But then I switched to my application that is built with tuscdotnet and the PATCH call started failing with an error about a missing server.

It turns out that tuscdotnet returns a relative path for location (Location: files/24e533e02ec3bc40c387f1a0e460e216) whereas the tusd implementation returns an absolute path (Location: https://tus.example.org/files/24e533e02ec3bc40c387f1a0e460e216).

According to the spec, either is acceptable, but the absolute is always shown in examples:

The Server MUST acknowledge a successful upload creation with the 201 Created status. The Server MUST set the Location header to the URL of the created resource. This URL MAY be absolute or relative.

So it seems that the location value needs to be checked and converted to an absolute path before the PATCH call. I'd attempt a PR, but I'm not familiar with golang at all.

An enhancement for files which have been uploaded?

Hi,

When I implement this example, I have met a problem: if the file has been uploaded, run this example again, it will hang there for ever. So my suggestion is in uploader.go, add the code similar like below. In this case, u.offset == u.upload.size at first calling but there is no signal to the chan.
image

[proposal] Add an option to perform a HEAD request before the upload

First of all, thanks for this very cool library!

I have a feature request/ proposal for the following usecase:

I have a custom server implementation, which will give the same upload endpoint, if it believes the files are the same (i.e. the POST /files/ redirects to /files/someHash for every file that matches a specific test - since those file are actually identical).

I can't solve this using the fingerprint system, since the two uploads might be done from two different computers and the fingerprint is computed by the server.

This library works fine if the upload not started before, but it doesn't work if a part of the file has been already uploaded (ErrOffsetMismatch).

I would like to add an option to the config (RequestOffsetOnUploadCreation) to tell the client to getUploadOffset on CreateUpload.

Change would be like:

diff --git a/client.go b/client.go
index b5923d1..1b43b2c 100644
--- a/client.go
+++ b/client.go
@@ -110,7 +110,17 @@ func (c *Client) CreateUpload(u *Upload) (*Uploader, error) {
                        c.Config.Store.Set(u.Fingerprint, url)
                }
 
-               return NewUploader(c, url, u, 0), nil
+               var offset int64
+
+               if c.Config.RequestOffsetOnUploadCreation {
+                       offset, err = c.getUploadOffset(url)
+
+                       if err != nil {
+                               return nil, err
+                       }
+               }
+
+               return NewUploader(c, url, u, offset), nil
        case 412:
                return nil, ErrVersionMismatch
        case 413:

What do you think ?

Can I make a PR with this change ? Do you have an idea for a name, better than RequestOffsetOnUploadCreation ?

Upload progress

Hi,

I've made some changes to go-tus to allow the Upload method to receive a channel. This way upload progress can be passed around. The struct for the info looks something like this.

type UploadInfo struct {
	Offset     int64
	TotalSize  int64
	Percentage int64
	Finished   bool
}

Is this something you would be willing to merge?

Resumable upload example

Hi,

is there a working example with resumable upload?

I want to loop over a directory and upload all files (new files are dynamically copied in the directory). If the connection to the server gets interrupted, I want that the tus client continues uploading once the connection is back. I guess, that is what the Resume flag in the config supports.

Thanks in advance,
David

Please tag and release this, so we can easily use it.

This needs a tag and release version number so that we (who must abide by corporate s/w quality rules) can actually use it.
Even if it's not percieved being ready for "release" (which I doubt) and just something like v0.0.1-alpha.

Yes I realise I can go get github.com/eventials/go-tus@05d0564bb571e81045012756065a8d002d717caf but that isn't really gonna fly.

We need to be able to get and/or reference it in our go.mod file like:

$ go get github.com/eventials/[email protected]

or
require (
    github.com/eventials/go-tus v0.0.1-alpha
)

Thanks.

Test cases not compatible with >v1.0.0 of tusd

Test cases in client_test.go not compatible with github.com/tus/tusd version >v1.0.0 as this version has some breaking changes compared to 0.14.0.

go test -v ./...
# github.com/eventials/go-tus
package github.com/eventials/go-tus (test)
	imports github.com/tus/tusd: no Go files in /gopath/src/github.com/tus/tusd
FAIL	github.com/eventials/go-tus [setup failed]
?   	github.com/eventials/go-tus/leveldbstore	[no test files]
?   	github.com/eventials/go-tus/memorystore	[no test files]
FAIL

Incorrect spelling of "Chunk"

In a handful of places the word "chunk" is spelled Chunck, which makes for an awkward public API call of uploader.UploadChunck.

It would be annoying to fix since its a breaking change, but it might still be nice to do so. Maybe if you didn't want to make a breaking API change you could just redirect the existing public functions that are spelled incorrectly and mark them as deprecated?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.