From a75c31e9ad33efec4b8f41b96e2ee90f387edd78 Mon Sep 17 00:00:00 2001 From: studiokaiji Date: Tue, 24 Oct 2023 23:15:41 +0900 Subject: [PATCH] =?UTF-8?q?filePathToUploadMediaRequest=E3=82=92=E5=88=86?= =?UTF-8?q?=E9=9B=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hostr/cmd/deploy/deploy.go | 96 +++++++------------------------------- hostr/cmd/deploy/media.go | 76 ++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 79 deletions(-) diff --git a/hostr/cmd/deploy/deploy.go b/hostr/cmd/deploy/deploy.go index c3b96aa..b71fa67 100644 --- a/hostr/cmd/deploy/deploy.go +++ b/hostr/cmd/deploy/deploy.go @@ -3,11 +3,8 @@ package deploy import ( "bufio" "bytes" - "encoding/base64" "encoding/json" "fmt" - "io" - "mime/multipart" "net/http" "net/url" "os" @@ -141,6 +138,12 @@ func convertLinks(priKey, pubKey, basePath string, replaceable bool, indexHtmlId continue } + // jsファイルを解析する + if strings.HasSuffix(basePath, ".js") { + jsContent := string(bytesContent) + + } + // Tagsを追加 tags := nostr.Tags{} // 置き換え可能なイベントの場合 @@ -176,82 +179,14 @@ func convertLinks(priKey, pubKey, basePath string, replaceable bool, indexHtmlId } else if slices.Contains(availableMediaHtmlTags, n.Data) { // 内部mediaファイルを対象にUpload Requestを作成 for i, a := range n.Attr { - if (a.Key == "href" || a.Key == "src") && !isExternalURL(a.Val) && isValidBasicFileType(a.Val) { + if (a.Key == "href" || a.Key == "src" || a.Key == "data") && !isExternalURL(a.Val) && isValidBasicFileType(a.Val) { filePath := filepath.Join(basePath, a.Val) - // ファイルを開く - file, err := os.Open(filePath) + // アップロードのためのHTTPリクエストを取得 + request, err := filePathToUploadMediaRequest(filePath, priKey, pubKey) if err != nil { - fmt.Printf("❌ Failed to read %s: %d", filePath, err) - continue + fmt.Println("❌ Failed generate upload request: ", err) } - defer file.Close() - - // リクエストボディのバッファを初期化 - var requestBody bytes.Buffer - // multipart writerを作成 - writer := multipart.NewWriter(&requestBody) - - // uploadtypeフィールドを設定 - err = writer.WriteField("uploadtype", "media") - if err != nil { - fmt.Printf("❌ Error writing field: %d", err) - continue - } - - // mediafileフィールドを作成 - part, err := writer.CreateFormFile("mediafile", filePath) - if err != nil { - fmt.Printf("❌ Error creating form file: %d", err) - continue - } - - // ファイルの内容をpartにコピー - _, err = io.Copy(part, file) - if err != nil { - fmt.Printf("❌ Error copying file: %d", err) - continue - } - - // writerを閉じてリクエストボディを完成させる - err = writer.Close() - if err != nil { - fmt.Printf("❌ Error closing writer: %d", err) - continue - } - - // タグを初期化 - tags := nostr.Tags{} - // タグを追加 - tags.AppendUnique(nostr.Tag{"u", uploadEndpoint}) - tags.AppendUnique(nostr.Tag{"method", "POST"}) - tags.AppendUnique(nostr.Tag{"payload", ""}) - - // イベントを取得 - ev, err := getEvent(priKey, pubKey, "", 27533, tags) - if err != nil { - fmt.Printf("❌ Error get event: %d", err) - continue - } - - // イベントをJSONにマーシャル - evJson, err := ev.MarshalJSON() - if err != nil { - fmt.Printf("❌ Error marshaling event: %d", err) - continue - } - - // HTTPリクエストを作成 - request, err := http.NewRequest("POST", uploadEndpoint, &requestBody) - if err != nil { - fmt.Printf("❌ Error creating request: %d", err) - continue - } - - // ヘッダーを設定 - request.Header.Set("Content-Type", writer.FormDataContentType()) - request.Header.Set("Authorization", "Nostr "+base64.StdEncoding.EncodeToString(evJson)) - request.Header.Set("Accept", "application/json") // アップロード処理を代入 uploadFunc := func() (*MediaResult, error) { @@ -259,8 +194,7 @@ func convertLinks(priKey, pubKey, basePath string, replaceable bool, indexHtmlId client := &http.Client{} response, err := client.Do(request) if err != nil { - fmt.Errorf("❌ Error sending request: %w", err) - + fmt.Errorf("Error sending request: %w", err) } defer response.Body.Close() @@ -268,12 +202,12 @@ func convertLinks(priKey, pubKey, basePath string, replaceable bool, indexHtmlId // ResultのDecode err = json.NewDecoder(response.Body).Decode(result) if err != nil { - return nil, fmt.Errorf("❌ Error decoding response: %w", err) + return nil, fmt.Errorf("Error decoding response: %w", err) } // アップロードに失敗した場合 if !result.result { - return nil, fmt.Errorf("❌ Failed to upload file: %w", err) + return nil, fmt.Errorf("Failed to upload file: %w", err) } // URLを割り当て @@ -294,3 +228,7 @@ func convertLinks(priKey, pubKey, basePath string, replaceable bool, indexHtmlId convertLinks(priKey, pubKey, basePath, replaceable, indexHtmlIdentifier, c) } } + +func convertLinksFromJS() { + +} diff --git a/hostr/cmd/deploy/media.go b/hostr/cmd/deploy/media.go index b293f88..5aca222 100644 --- a/hostr/cmd/deploy/media.go +++ b/hostr/cmd/deploy/media.go @@ -1,7 +1,13 @@ package deploy import ( + "bytes" + "encoding/base64" "fmt" + "io" + "mime/multipart" + "net/http" + "os" "strings" "sync" @@ -118,3 +124,73 @@ func uploadMediaFilesFromQueue() { wg.Wait() } + +func filePathToUploadMediaRequest(filePath, priKey, pubKey string) (*http.Request, error) { + // ファイルを開く + file, err := os.Open(filePath) + if err != nil { + return nil, fmt.Errorf("Failed to read %s: %w", filePath, err) + } + defer file.Close() + + // リクエストボディのバッファを初期化 + var requestBody bytes.Buffer + // multipart writerを作成 + writer := multipart.NewWriter(&requestBody) + + // uploadtypeフィールドを設定 + err = writer.WriteField("uploadtype", "media") + if err != nil { + return nil, fmt.Errorf("Error writing field: %w", err) + } + + // mediafileフィールドを作成 + part, err := writer.CreateFormFile("mediafile", filePath) + if err != nil { + return nil, fmt.Errorf("Error creating form file: %w", err) + } + + // ファイルの内容をpartにコピー + _, err = io.Copy(part, file) + if err != nil { + return nil, fmt.Errorf("Error copying file: %w", err) + } + + // writerを閉じてリクエストボディを完成させる + err = writer.Close() + if err != nil { + return nil, fmt.Errorf("Error closing writer: %w", err) + } + + // タグを初期化 + tags := nostr.Tags{} + // タグを追加 + tags.AppendUnique(nostr.Tag{"u", uploadEndpoint}) + tags.AppendUnique(nostr.Tag{"method", "POST"}) + tags.AppendUnique(nostr.Tag{"payload", ""}) + + // イベントを生成 + ev, err := getEvent(priKey, pubKey, "", 27533, tags) + if err != nil { + return nil, fmt.Errorf("Error get event: %d", err) + } + + // イベントをJSONにマーシャル + evJson, err := ev.MarshalJSON() + if err != nil { + return nil, fmt.Errorf("Error marshaling event: %d", err) + } + + // HTTPリクエストを作成 + request, err := http.NewRequest("POST", uploadEndpoint, &requestBody) + if err != nil { + return nil, fmt.Errorf("Error creating request: %d", err) + } + + // ヘッダーを設定 + request.Header.Set("Content-Type", writer.FormDataContentType()) + request.Header.Set("Authorization", "Nostr "+base64.StdEncoding.EncodeToString(evJson)) + request.Header.Set("Accept", "application/json") + + return request, nil +}