Repository: usememos/memos
File: api/v1/resource.go (Go)

Code before:

	return store.Private, nil
}

func convertResourceFromStore(resource *store.Resource) *Resource {
	return &Resource{
		ID:               resource.ID,
		CreatorID:        resource.CreatorID,
		CreatedTs:        resource.CreatedTs,
		UpdatedTs:        resource.UpdatedTs,
		Filename:         resource.Filename,
		Blob:             resource.Blob,
		InternalPath:     resource.InternalPath,
		ExternalLink:     resource.ExternalLink,
		Type:             resource.Type,
		Size:             resource.Size,
		LinkedMemoAmount: resource.LinkedMemoAmount,
	}
}

// SaveResourceBlob save the blob of resource based on the storage config
//
// Depend on the storage config, some fields of *store.ResourceCreate will be changed:
// 1. *DatabaseStorage*: `create.Blob`.
// 2. *LocalStorage*: `create.InternalPath`.
// 3. Others( extern service): `create.ExternalLink`.
func SaveResourceBlob(ctx context.Context, s *store.Store, create *store.Resource, r io.Reader) error {
	systemSettingStorageServiceID, err := s.GetSystemSetting(ctx, &store.FindSystemSetting{Name: SystemSettingStorageServiceIDName.String()})
	if err != nil {
		return fmt.Errorf("Failed to find SystemSettingStorageServiceIDName: %s", err)
	}

	storageServiceID := DatabaseStorage
	if systemSettingStorageServiceID != nil {
		err = json.Unmarshal([]byte(systemSettingStorageServiceID.Value), &storageServiceID)
		if err != nil {
			return fmt.Errorf("Failed to unmarshal storage service id: %s", err)
		}
	}

	// `DatabaseStorage` means store blob into database
	if storageServiceID == DatabaseStorage {
		fileBytes, err := io.ReadAll(r)
		if err != nil {
			return fmt.Errorf("Failed to read file: %s", err)
		}
		create.Blob = fileBytes
		return nil
	}

	// `LocalStorage` means save blob into local disk

Review comment (suggestion):
```suggestion
// 3. Others(external service): `create.ExternalLink`.
```

Code after:

	return store.Private, nil
}

func convertResourceFromStore(resource *store.Resource) *Resource {
	return &Resource{
		ID:               resource.ID,
		CreatorID:        resource.CreatorID,
		CreatedTs:        resource.CreatedTs,
		UpdatedTs:        resource.UpdatedTs,
		Filename:         resource.Filename,
		Blob:             resource.Blob,
		InternalPath:     resource.InternalPath,
		ExternalLink:     resource.ExternalLink,
		Type:             resource.Type,
		Size:             resource.Size,
		LinkedMemoAmount: resource.LinkedMemoAmount,
	}
}

// SaveResourceBlob save the blob of resource based on the storage config
//
// Depend on the storage config, some fields of *store.ResourceCreate will be changed:
// 1. *DatabaseStorage*: `create.Blob`.
// 2. *LocalStorage*: `create.InternalPath`.
// 3. Others( external service): `create.ExternalLink`.
func SaveResourceBlob(ctx context.Context, s *store.Store, create *store.Resource, r io.Reader) error {
	systemSettingStorageServiceID, err := s.GetSystemSetting(ctx, &store.FindSystemSetting{Name: SystemSettingStorageServiceIDName.String()})
	if err != nil {
		return fmt.Errorf("Failed to find SystemSettingStorageServiceIDName: %s", err)
	}

	storageServiceID := DatabaseStorage
	if systemSettingStorageServiceID != nil {
		err = json.Unmarshal([]byte(systemSettingStorageServiceID.Value), &storageServiceID)
		if err != nil {
			return fmt.Errorf("Failed to unmarshal storage service id: %s", err)
		}
	}

	// `DatabaseStorage` means store blob into database
	if storageServiceID == DatabaseStorage {
		fileBytes, err := io.ReadAll(r)
		if err != nil {
			return fmt.Errorf("Failed to read file: %s", err)
		}
		create.Blob = fileBytes
		return nil
	}

	// `LocalStorage` means save blob into local disk