mirror of
https://github.com/studiokaiji/nostr-webhost.git
synced 2026-01-31 12:44:42 +01:00
Nostr特有のロジックを分離
This commit is contained in:
@@ -3,144 +3,31 @@ package deploy
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/nbd-wtf/go-nostr"
|
||||
"github.com/nbd-wtf/go-nostr/nip19"
|
||||
"github.com/studiokaiji/nostr-webhost/hostr/cmd/consts"
|
||||
"github.com/studiokaiji/nostr-webhost/hostr/cmd/keystore"
|
||||
"github.com/studiokaiji/nostr-webhost/hostr/cmd/relays"
|
||||
"github.com/studiokaiji/nostr-webhost/hostr/cmd/tools"
|
||||
"golang.org/x/exp/slices"
|
||||
"golang.org/x/net/html"
|
||||
)
|
||||
|
||||
func pathToKind(path string, replaceable bool) (int, error) {
|
||||
// パスを分割
|
||||
separatedPath := strings.Split(path, ".")
|
||||
// 拡張子を取得
|
||||
ex := separatedPath[len(separatedPath)-1]
|
||||
// replaceable(NIP-33)の場合はReplaceableなkindを返す
|
||||
switch ex {
|
||||
case "html":
|
||||
if replaceable {
|
||||
return consts.KindWebhostHTML, nil
|
||||
} else {
|
||||
return consts.KindWebhostReplaceableHTML, nil
|
||||
}
|
||||
case "css":
|
||||
if replaceable {
|
||||
return consts.KindWebhostReplaceableCSS, nil
|
||||
} else {
|
||||
return consts.KindWebhostCSS, nil
|
||||
}
|
||||
case "js":
|
||||
if replaceable {
|
||||
return consts.KindWebhostReplaceableJS, nil
|
||||
} else {
|
||||
return consts.KindWebhostJS, nil
|
||||
}
|
||||
default:
|
||||
return 0, fmt.Errorf("Invalid path")
|
||||
}
|
||||
}
|
||||
|
||||
// Replaceableにする場合のidentifier(dタグ)を取得
|
||||
func getReplaceableIdentifier(indexHtmlIdentifier, filePath string) string {
|
||||
return indexHtmlIdentifier + "/" + filePath[1:]
|
||||
}
|
||||
|
||||
var nostrEventsQueue []*nostr.Event
|
||||
|
||||
func addNostrEventQueue(event *nostr.Event) {
|
||||
nostrEventsQueue = append(nostrEventsQueue, event)
|
||||
}
|
||||
|
||||
var allRelays []string
|
||||
|
||||
func publishEventsFromQueue(replaceable bool) (string, string) {
|
||||
ctx := context.Background()
|
||||
|
||||
fmt.Println("Publishing...")
|
||||
|
||||
// 各リレーに接続
|
||||
var relays []*nostr.Relay
|
||||
|
||||
for _, url := range allRelays {
|
||||
relay, err := nostr.RelayConnect(ctx, url)
|
||||
if err != nil {
|
||||
fmt.Println("❌ Failed to connect to:", url)
|
||||
continue
|
||||
}
|
||||
relays = append(relays, relay)
|
||||
}
|
||||
|
||||
// Publishの進捗状況を表示
|
||||
allEventsCount := len(nostrEventsQueue)
|
||||
uploadedFilesCount := 0
|
||||
|
||||
var wg sync.WaitGroup
|
||||
|
||||
go func() {
|
||||
wg.Add(1)
|
||||
tools.DisplayProgressBar(&uploadedFilesCount, &allEventsCount)
|
||||
wg.Done()
|
||||
}()
|
||||
|
||||
var mutex sync.Mutex
|
||||
|
||||
// リレーへpublish
|
||||
for _, ev := range nostrEventsQueue {
|
||||
wg.Add(1)
|
||||
go func(event *nostr.Event) {
|
||||
for _, relay := range relays {
|
||||
_, err := relay.Publish(ctx, *event)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
continue
|
||||
}
|
||||
}
|
||||
mutex.Lock() // ロックして排他制御
|
||||
uploadedFilesCount++ // カウントアップ
|
||||
mutex.Unlock() // ロック解除
|
||||
wg.Done() // ゴルーチンの終了を通知
|
||||
}(ev)
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
|
||||
if uploadedFilesCount < allEventsCount {
|
||||
fmt.Println("Failed to deploy", allEventsCount-uploadedFilesCount, "files.")
|
||||
}
|
||||
|
||||
indexEvent := nostrEventsQueue[len(nostrEventsQueue)-1]
|
||||
|
||||
encoded := ""
|
||||
if !replaceable {
|
||||
if enc, err := nip19.EncodeEvent(indexEvent.ID, allRelays, indexEvent.PubKey); err == nil {
|
||||
encoded = enc
|
||||
} else {
|
||||
fmt.Println("❌ Failed to covert nevent:", err)
|
||||
}
|
||||
}
|
||||
|
||||
return indexEvent.ID, encoded
|
||||
}
|
||||
|
||||
func isExternalURL(urlStr string) bool {
|
||||
u, err := url.Parse(urlStr)
|
||||
return err == nil && u.Scheme != "" && u.Host != ""
|
||||
}
|
||||
|
||||
func isValidBasicFileType(str string) bool {
|
||||
return strings.HasSuffix(str, ".html") || strings.HasSuffix(str, ".css") || strings.HasSuffix(str, ".js")
|
||||
}
|
||||
|
||||
func Deploy(basePath string, replaceable bool, htmlIdentifier string) (string, string, error) {
|
||||
// 引数からデプロイしたいサイトのパスを受け取る。
|
||||
filePath := filepath.Join(basePath, "index.html")
|
||||
@@ -193,6 +80,13 @@ func Deploy(basePath string, replaceable bool, htmlIdentifier string) (string, s
|
||||
// リンクの解析と変換
|
||||
convertLinks(priKey, pubKey, basePath, replaceable, htmlIdentifier, doc)
|
||||
|
||||
if len(mediaUploadRequestQueue) > 0 {
|
||||
// メディアのアップロード
|
||||
fmt.Println("📷 Uploading media files")
|
||||
uploadMediaFilesFromQueue()
|
||||
fmt.Println("📷 Media upload finished.")
|
||||
}
|
||||
|
||||
// 更新されたHTML
|
||||
var buf bytes.Buffer
|
||||
html.Render(&buf, doc)
|
||||
@@ -226,55 +120,170 @@ func Deploy(basePath string, replaceable bool, htmlIdentifier string) (string, s
|
||||
}
|
||||
|
||||
func convertLinks(priKey, pubKey, basePath string, replaceable bool, indexHtmlIdentifier string, n *html.Node) {
|
||||
// <link> と <script> タグを対象とする
|
||||
if n.Type == html.ElementNode && (n.Data == "link" || n.Data == "script") {
|
||||
for i, a := range n.Attr {
|
||||
// href,srcのうち、外部URLでないものかつ. html, .css, .js のみ変換する
|
||||
if (a.Key == "href" || a.Key == "src") && !isExternalURL(a.Val) && isValidBasicFileType(a.Val) {
|
||||
filePath := filepath.Join(basePath, a.Val)
|
||||
if n.Type == html.ElementNode {
|
||||
if n.Data == "link" || n.Data == "script" {
|
||||
// <link> と <script> タグを対象としてNostr Eventを作成
|
||||
for i, a := range n.Attr {
|
||||
// href,srcのうち、外部URLでないものかつ. html, .css, .js のみ変換する
|
||||
if (a.Key == "href" || a.Key == "src") && !isExternalURL(a.Val) && isValidBasicFileType(a.Val) {
|
||||
filePath := filepath.Join(basePath, a.Val)
|
||||
|
||||
// kindを取得
|
||||
kind, err := pathToKind(filePath, replaceable)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
// contentを取得
|
||||
bytesContent, err := os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
fmt.Println("❌ Failed to read", filePath, ":", err)
|
||||
continue
|
||||
}
|
||||
|
||||
// Tagsを追加
|
||||
tags := nostr.Tags{}
|
||||
// 置き換え可能なイベントの場合
|
||||
if replaceable {
|
||||
fileIdentifier := getReplaceableIdentifier(indexHtmlIdentifier, a.Val)
|
||||
tags = tags.AppendUnique(nostr.Tag{"d", fileIdentifier})
|
||||
// 元のパスをfileIdentifierに置き換える
|
||||
n.Attr[i].Val = fileIdentifier
|
||||
}
|
||||
|
||||
// Eventを生成し、キューに追加
|
||||
event, err := getEvent(priKey, pubKey, string(bytesContent), kind, tags)
|
||||
if err != nil {
|
||||
fmt.Println("❌ Failed to get event for", filePath, ":", err)
|
||||
break
|
||||
}
|
||||
|
||||
addNostrEventQueue(event)
|
||||
fmt.Println("Added", filePath, "event to publish queue")
|
||||
|
||||
// 置き換え可能なイベントでない場合
|
||||
if !replaceable {
|
||||
// neventを指定
|
||||
nevent, err := nip19.EncodeEvent(event.ID, allRelays, pubKey)
|
||||
// kindを取得
|
||||
kind, err := pathToKind(filePath, replaceable)
|
||||
if err != nil {
|
||||
fmt.Println("❌ Failed to encode event", filePath, ":", err)
|
||||
continue
|
||||
}
|
||||
|
||||
// contentを取得
|
||||
bytesContent, err := os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
fmt.Println("❌ Failed to read", filePath, ":", err)
|
||||
continue
|
||||
}
|
||||
|
||||
// Tagsを追加
|
||||
tags := nostr.Tags{}
|
||||
// 置き換え可能なイベントの場合
|
||||
if replaceable {
|
||||
fileIdentifier := getReplaceableIdentifier(indexHtmlIdentifier, a.Val)
|
||||
tags = tags.AppendUnique(nostr.Tag{"d", fileIdentifier})
|
||||
// 元のパスをfileIdentifierに置き換える
|
||||
n.Attr[i].Val = fileIdentifier
|
||||
}
|
||||
|
||||
// Eventを生成し、キューに追加
|
||||
event, err := getEvent(priKey, pubKey, string(bytesContent), kind, tags)
|
||||
if err != nil {
|
||||
fmt.Println("❌ Failed to get event for", filePath, ":", err)
|
||||
break
|
||||
}
|
||||
n.Attr[i].Val = nevent
|
||||
|
||||
addNostrEventQueue(event)
|
||||
fmt.Println("Added", filePath, "event to publish queue")
|
||||
|
||||
// 置き換え可能なイベントでない場合
|
||||
if !replaceable {
|
||||
// neventを指定
|
||||
nevent, err := nip19.EncodeEvent(event.ID, allRelays, pubKey)
|
||||
if err != nil {
|
||||
fmt.Println("❌ Failed to encode event", filePath, ":", err)
|
||||
break
|
||||
}
|
||||
n.Attr[i].Val = nevent
|
||||
}
|
||||
}
|
||||
}
|
||||
} 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) {
|
||||
filePath := filepath.Join(basePath, a.Val)
|
||||
|
||||
// ファイルを開く
|
||||
file, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
fmt.Printf("❌ Failed to read %s: %d", filePath, err)
|
||||
continue
|
||||
}
|
||||
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) {
|
||||
// リクエストを送信
|
||||
client := &http.Client{}
|
||||
response, err := client.Do(request)
|
||||
if err != nil {
|
||||
fmt.Errorf("❌ Error sending request: %w", err)
|
||||
|
||||
}
|
||||
defer response.Body.Close()
|
||||
|
||||
var result *MediaResult
|
||||
// ResultのDecode
|
||||
err = json.NewDecoder(response.Body).Decode(result)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("❌ Error decoding response: %w", err)
|
||||
}
|
||||
|
||||
// アップロードに失敗した場合
|
||||
if !result.result {
|
||||
return nil, fmt.Errorf("❌ Failed to upload file: %w", err)
|
||||
}
|
||||
|
||||
// URLを割り当て
|
||||
n.Attr[i].Val = result.url
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// Queueにアップロード処理を追加
|
||||
addMediaUploadRequestFuncQueue(uploadFunc)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -285,20 +294,3 @@ func convertLinks(priKey, pubKey, basePath string, replaceable bool, indexHtmlId
|
||||
convertLinks(priKey, pubKey, basePath, replaceable, indexHtmlIdentifier, c)
|
||||
}
|
||||
}
|
||||
|
||||
func getEvent(priKey, pubKey, content string, kind int, tags nostr.Tags) (*nostr.Event, error) {
|
||||
ev := nostr.Event{
|
||||
PubKey: pubKey,
|
||||
CreatedAt: nostr.Now(),
|
||||
Kind: kind,
|
||||
Content: content,
|
||||
Tags: tags,
|
||||
}
|
||||
|
||||
err := ev.Sign(priKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &ev, err
|
||||
}
|
||||
|
||||
139
hostr/cmd/deploy/nostr.go
Normal file
139
hostr/cmd/deploy/nostr.go
Normal file
@@ -0,0 +1,139 @@
|
||||
package deploy
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/nbd-wtf/go-nostr"
|
||||
"github.com/nbd-wtf/go-nostr/nip19"
|
||||
"github.com/studiokaiji/nostr-webhost/hostr/cmd/consts"
|
||||
"github.com/studiokaiji/nostr-webhost/hostr/cmd/tools"
|
||||
)
|
||||
|
||||
func getEvent(priKey, pubKey, content string, kind int, tags nostr.Tags) (*nostr.Event, error) {
|
||||
ev := nostr.Event{
|
||||
PubKey: pubKey,
|
||||
CreatedAt: nostr.Now(),
|
||||
Kind: kind,
|
||||
Content: content,
|
||||
Tags: tags,
|
||||
}
|
||||
|
||||
err := ev.Sign(priKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &ev, err
|
||||
}
|
||||
|
||||
func isValidBasicFileType(str string) bool {
|
||||
return strings.HasSuffix(str, ".html") || strings.HasSuffix(str, ".css") || strings.HasSuffix(str, ".js")
|
||||
}
|
||||
func publishEventsFromQueue(replaceable bool) (string, string) {
|
||||
ctx := context.Background()
|
||||
|
||||
fmt.Println("Publishing...")
|
||||
|
||||
// 各リレーに接続
|
||||
var relays []*nostr.Relay
|
||||
|
||||
for _, url := range allRelays {
|
||||
relay, err := nostr.RelayConnect(ctx, url)
|
||||
if err != nil {
|
||||
fmt.Println("❌ Failed to connect to:", url)
|
||||
continue
|
||||
}
|
||||
relays = append(relays, relay)
|
||||
}
|
||||
|
||||
// Publishの進捗状況を表示
|
||||
allEventsCount := len(nostrEventsQueue)
|
||||
uploadedFilesCount := 0
|
||||
|
||||
var wg sync.WaitGroup
|
||||
|
||||
go func() {
|
||||
wg.Add(1)
|
||||
tools.DisplayProgressBar(&uploadedFilesCount, &allEventsCount)
|
||||
wg.Done()
|
||||
}()
|
||||
|
||||
var mutex sync.Mutex
|
||||
|
||||
// リレーへpublish
|
||||
for _, ev := range nostrEventsQueue {
|
||||
wg.Add(1)
|
||||
go func(event *nostr.Event) {
|
||||
for _, relay := range relays {
|
||||
_, err := relay.Publish(ctx, *event)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
continue
|
||||
}
|
||||
}
|
||||
mutex.Lock() // ロックして排他制御
|
||||
uploadedFilesCount++ // カウントアップ
|
||||
mutex.Unlock() // ロック解除
|
||||
wg.Done() // ゴルーチンの終了を通知
|
||||
}(ev)
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
|
||||
if uploadedFilesCount < allEventsCount {
|
||||
fmt.Println("Failed to deploy", allEventsCount-uploadedFilesCount, "files.")
|
||||
}
|
||||
|
||||
indexEvent := nostrEventsQueue[len(nostrEventsQueue)-1]
|
||||
|
||||
encoded := ""
|
||||
if !replaceable {
|
||||
if enc, err := nip19.EncodeEvent(indexEvent.ID, allRelays, indexEvent.PubKey); err == nil {
|
||||
encoded = enc
|
||||
} else {
|
||||
fmt.Println("❌ Failed to covert nevent:", err)
|
||||
}
|
||||
}
|
||||
|
||||
return indexEvent.ID, encoded
|
||||
}
|
||||
|
||||
func pathToKind(path string, replaceable bool) (int, error) {
|
||||
// パスを分割
|
||||
separatedPath := strings.Split(path, ".")
|
||||
// 拡張子を取得
|
||||
ex := separatedPath[len(separatedPath)-1]
|
||||
// replaceable(NIP-33)の場合はReplaceableなkindを返す
|
||||
switch ex {
|
||||
case "html":
|
||||
if replaceable {
|
||||
return consts.KindWebhostHTML, nil
|
||||
} else {
|
||||
return consts.KindWebhostReplaceableHTML, nil
|
||||
}
|
||||
case "css":
|
||||
if replaceable {
|
||||
return consts.KindWebhostReplaceableCSS, nil
|
||||
} else {
|
||||
return consts.KindWebhostCSS, nil
|
||||
}
|
||||
case "js":
|
||||
if replaceable {
|
||||
return consts.KindWebhostReplaceableJS, nil
|
||||
} else {
|
||||
return consts.KindWebhostJS, nil
|
||||
}
|
||||
default:
|
||||
return 0, fmt.Errorf("Invalid path")
|
||||
}
|
||||
}
|
||||
|
||||
// Replaceableにする場合のidentifier(dタグ)を取得
|
||||
func getReplaceableIdentifier(indexHtmlIdentifier, filePath string) string {
|
||||
return indexHtmlIdentifier + "/" + filePath[1:]
|
||||
}
|
||||
|
||||
var nostrEventsQueue []*nostr.Event
|
||||
Reference in New Issue
Block a user