tus / tusd

Reference server implementation in Go of tus: the open protocol for resumable file uploads
https://tus.github.io/tusd
MIT License
2.92k stars 465 forks source link

-upload-dir flag #1115

Closed jaejungkim closed 1 week ago

jaejungkim commented 2 months ago

Question I want to save the upload file in a format that is separately saved by the year/month at the time of upload, to the location specified by -upload-dir flag when uploading files. Is it possible?

Setup details Please provide following details, if applicable to your situation:

Acconut commented 2 months ago

tusd does not have such a feature on its own but you can implement it using its hook system. You can write a pre-create hook that returns an custom ID that includes the year and month, plus a random specifier. This ID will then be used as the file name.

jaejungkim commented 2 months ago

I'm currently using tusd version v1.5.0, is it possible to have the returns an custome ID of the pre-create file hook you mentioned? But according to the documents?
Upload ID will be null for pre-create hook. Did you say that? How do I return a custom ID using a simple pre-create hook script or a verifiable example where? The pre-create of the official site example doesn't return a custome ID? I hope you understand that my English is not good enough

Acconut commented 2 months ago

I'm currently using tusd version v1.5.0, is it possible to have the returns an custome ID of the pre-create file hook you mentioned?

No, this feature is only available since tusd v2.0.0. You can have a look at the release notes to see what changed.

For example implementations of hooks, have a look at https://github.com/tus/tusd/tree/main/examples. For example, here is a HTTP hook server implemented in Python that returns a custom ID:

https://github.com/tus/tusd/blob/15f05c021c9770dc0cd2818aa4ef44b9e361f5b2/examples/hooks/http/server.py#L47

jaejungkim commented 2 months ago

Isn't hooks-http not able to save the file format that is uploaded in the first place as upload-dir/year/month/uuidV4? I wonder if it is possible to do that with a pre-create file hook located in hooks-dir, but it is not possible at the moment in v2.0?

Acconut commented 2 months ago

I don't understand what you mean. Every hook implementation (HTTP, file-based, gRPC, plugin) is able to return a custom ID. We have a HTTP-based example for this (-hooks-http), but no example using the file-based hooks (-hooks-dir).

jaejungkim commented 2 months ago

what I need is to know how to return a custom ID from the pre-create script in -hooks-dir. Where can I refer to this?

Acconut commented 2 months ago

Here is a simple Python script that should produce the desired result:

from datetime import datetime
import uuid
import json

# Get current year and month
current_year = datetime.now().year
current_month = datetime.now().month

# Generate a random UUID
random_uuid = uuid.uuid4()

# Construct the JSON object
json_object = {
    "ChangeFileInfo": {
        "ID": f"{current_year}/{current_month}/{random_uuid}",
    }
}

# Convert the Python dictionary to a JSON formatted string and print it
json_output = json.dumps(json_object, indent=4)
print(json_output)

I haven't tested it though. For information about how file hooks work in general, please read https://tus.github.io/tusd/advanced-topics/hooks/#file-hooks