Skip to content

Commit e5a6c01

Browse files
committed
feat: support ipfs
1 parent e702a56 commit e5a6c01

File tree

9 files changed

+705
-121
lines changed

9 files changed

+705
-121
lines changed

api/jiaozifs.gen.go

+71-33
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/swagger.yml

+5
Original file line numberDiff line numberDiff line change
@@ -1607,6 +1607,11 @@ paths:
16071607
- repo
16081608
operationId: deleteRepository
16091609
summary: delete repository
1610+
parameters:
1611+
- in: query
1612+
name: is_clean_data
1613+
schema:
1614+
type: boolean
16101615
responses:
16111616
200:
16121617
description: success to delete repository

block/adapter.go

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const (
3333
BlockstoreTypeGS = "gs"
3434
BlockstoreTypeAzure = "azure"
3535
BlockstoreTypeLocal = "local"
36+
BlockstoreIPFS = "ipfs"
3637
BlockstoreTypeMem = "mem"
3738
BlockstoreTypeTransient = "transient"
3839
)

block/factory/build.go

+20
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"context"
55
"fmt"
66

7+
"github.com/jiaozifs/jiaozifs/block/ipfs"
8+
79
"cloud.google.com/go/storage"
810
"github.com/aws/aws-sdk-go-v2/service/s3"
911
logging "github.com/ipfs/go-log/v2"
@@ -39,6 +41,12 @@ func BuildBlockAdapter(ctx context.Context, c params.AdapterConfig) (block.Adapt
3941
return nil, err
4042
}
4143
return buildLocalAdapter(ctx, p)
44+
case block.BlockstoreIPFS:
45+
p, err := c.BlockstoreIpfsParams()
46+
if err != nil {
47+
return nil, err
48+
}
49+
return buildIpfsAdapter(ctx, p)
4250
case block.BlockstoreTypeS3:
4351
p, err := c.BlockstoreS3Params()
4452
if err != nil {
@@ -67,6 +75,18 @@ func BuildBlockAdapter(ctx context.Context, c params.AdapterConfig) (block.Adapt
6775
}
6876
}
6977

78+
func buildIpfsAdapter(_ context.Context, params params.Ipfs) (*ipfs.Adapter, error) {
79+
adapter, err := ipfs.NewAdapter(params.Url)
80+
if err != nil {
81+
return nil, fmt.Errorf("got error opening a local block adapter with path %s: %w", params.Url, err)
82+
}
83+
log.With(
84+
"type", "local",
85+
"url", params.Url,
86+
).Info("initialized blockstore adapter")
87+
return adapter, nil
88+
}
89+
7090
func buildLocalAdapter(_ context.Context, params params.Local) (*local.Adapter, error) {
7191
adapter, err := local.NewAdapter(params.Path,
7292
local.WithAllowedExternalPrefixes(params.AllowedExternalPrefixes),

block/params/block.go

+5
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,16 @@ type AdapterConfig interface {
1010
BlockstoreLocalParams() (Local, error)
1111
BlockstoreS3Params() (S3, error)
1212
BlockstoreGSParams() (GS, error)
13+
BlockstoreIpfsParams() (Ipfs, error)
1314
BlockstoreAzureParams() (Azure, error)
1415
}
1516

1617
type Mem struct{}
1718

19+
type Ipfs struct {
20+
Url string
21+
}
22+
1823
type Local struct {
1924
Path string
2025
ImportEnabled bool

config/blockstore.go

+9
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ type BlockStoreConfig struct {
1818
ImportHidden bool `mapstructure:"import_hidden" json:"import_hidden"`
1919
AllowedExternalPrefixes []string `mapstructure:"allowed_external_prefixes" json:"allowed_external_prefixes"`
2020
} `mapstructure:"local" json:"local"`
21+
Ipfs *struct {
22+
Url string `mapstructure:"url" json:"url"`
23+
} `mapstructure:"ipfs" json:"ipfs"`
2124
S3 *struct {
2225
S3AuthInfo `mapstructure:",squash"`
2326
Region string `mapstructure:"region" json:"region"`
@@ -62,6 +65,12 @@ func (c *BlockStoreConfig) BlockstoreType() string {
6265
return c.Type
6366
}
6467

68+
func (c *BlockStoreConfig) BlockstoreIpfsParams() (params.Ipfs, error) {
69+
return params.Ipfs{
70+
Url: c.Ipfs.Url,
71+
}, nil
72+
}
73+
6574
func (c *BlockStoreConfig) BlockstoreS3Params() (params.S3, error) {
6675
var webIdentity *params.S3WebIdentity
6776
if c.S3.WebIdentity != nil {

controller/repository_ctl.go

+19-2
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ func (repositoryCtl RepositoryController) CreateRepository(ctx context.Context,
163163
w.Forbidden()
164164
return
165165
}
166-
storageNamespace = cfg.DefaultNamespacePrefix
166+
storageNamespace = utils.String(fmt.Sprintf("%s://%s", cfg.BlockstoreType(), repoID.String()))
167167
} else {
168168
storageNamespace = utils.String(fmt.Sprintf("%s://%s", repositoryCtl.PublicStorageConfig.BlockstoreType(), repoID.String()))
169169
}
@@ -208,7 +208,7 @@ func (repositoryCtl RepositoryController) CreateRepository(ctx context.Context,
208208
w.JSON(repositoryToDto(createdRepo))
209209
}
210210

211-
func (repositoryCtl RepositoryController) DeleteRepository(ctx context.Context, w *api.JiaozifsResponse, _ *http.Request, ownerName string, repositoryName string) {
211+
func (repositoryCtl RepositoryController) DeleteRepository(ctx context.Context, w *api.JiaozifsResponse, _ *http.Request, ownerName string, repositoryName string, params api.DeleteRepositoryParams) {
212212
operator, err := auth.GetOperator(ctx)
213213
if err != nil {
214214
w.Error(err)
@@ -287,6 +287,23 @@ func (repositoryCtl RepositoryController) DeleteRepository(ctx context.Context,
287287
w.Error(err)
288288
return
289289
}
290+
} else if utils.BoolValue(params.IsCleanData) {
291+
cfg := config.BlockStoreConfig{}
292+
err = json.Unmarshal([]byte(utils.StringValue(repository.StorageAdapterParams)), &cfg)
293+
if err != nil {
294+
w.Error(err)
295+
return
296+
}
297+
adapter, err := factory.BuildBlockAdapter(ctx, &cfg)
298+
if err != nil {
299+
w.Error(err)
300+
return
301+
}
302+
err = adapter.RemoveNameSpace(ctx, *repository.StorageNamespace)
303+
if err != nil {
304+
w.Error(err)
305+
return
306+
}
290307
}
291308

292309
w.OK()

0 commit comments

Comments
 (0)