github.com/uber/kraken@v0.1.4/lib/store/metadata/torrentmeta.go (about)

     1  // Copyright (c) 2016-2019 Uber Technologies, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  package metadata
    15  
    16  import (
    17  	"regexp"
    18  
    19  	"github.com/uber/kraken/core"
    20  )
    21  
    22  const _torrentMetaSuffix = "_torrentmeta"
    23  
    24  func init() {
    25  	Register(regexp.MustCompile(_torrentMetaSuffix), &torrentMetaFactory{})
    26  }
    27  
    28  type torrentMetaFactory struct{}
    29  
    30  func (f torrentMetaFactory) Create(suffix string) Metadata {
    31  	return &TorrentMeta{}
    32  }
    33  
    34  // TorrentMeta wraps torrent metainfo storage as metadata.
    35  type TorrentMeta struct {
    36  	MetaInfo *core.MetaInfo
    37  }
    38  
    39  // NewTorrentMeta return a new TorrentMeta.
    40  func NewTorrentMeta(mi *core.MetaInfo) *TorrentMeta {
    41  	return &TorrentMeta{mi}
    42  }
    43  
    44  // GetSuffix returns a static suffix.
    45  func (m *TorrentMeta) GetSuffix() string {
    46  	return _torrentMetaSuffix
    47  }
    48  
    49  // Movable is true.
    50  func (m *TorrentMeta) Movable() bool {
    51  	return true
    52  }
    53  
    54  // Serialize converts m to bytes.
    55  func (m *TorrentMeta) Serialize() ([]byte, error) {
    56  	return m.MetaInfo.Serialize()
    57  }
    58  
    59  // Deserialize loads b into m.
    60  func (m *TorrentMeta) Deserialize(b []byte) error {
    61  	mi, err := core.DeserializeMetaInfo(b)
    62  	if err != nil {
    63  		return err
    64  	}
    65  	m.MetaInfo = mi
    66  	return nil
    67  }