示例#1
0
文件: cache.go 项目: jash16/download
func (m MetaCache) ProcessEvent(cev *cacheEvent) error {
	typ := cev.eventType
	file := cev.filename
	//fmt.Printf("get metaInfo modify: %d, %s\n", typ, file)
	switch typ {
	case ADD, MOD:
		md5, err := common.CalFileMd5(file)
		if err != nil {
			return err
		}
		fileSize, err := common.GetFileSize(file)
		if err != nil {
			return err
		}
		metaInfo := &MetaInfo{
			FileName: file,
			FileSize: fileSize,
			Md5Info:  md5,
		}
		m[file] = metaInfo
		delete(m, file)
	case DEL:
		if _, ok := m[file]; ok {
			delete(m, file)
		}
	}
	return nil
}
示例#2
0
func (p *protocolV1) SendMetaInfo(client *Client, file string) error {
	fullFile := p.ctx.s.Opts.DataPath + file
	var metaInfo *MetaInfo
	var md5s string
	var fileSize int64
	var err error
	metaInfo = p.ctx.s.GetMetaInfo(file)
	if metaInfo == nil {
		p.ctx.s.logf("get requeset for file: %s", fullFile)
		md5s, err = common.CalFileMd5(fullFile)
		if err != nil {
			return err
		}
		fileSize, err = common.GetFileSize(fullFile)
		if err != nil {
			return err
		}
		metaInfo = &MetaInfo{
			FileName: file,
			FileSize: fileSize,
			Md5Info:  md5s,
		}
		p.ctx.s.AddMetaCache(file, metaInfo)
		//p.ctx.s.md5Cache[file] = metaInfo
	} else {
		md5s = metaInfo.Md5Info
		fileSize = metaInfo.FileSize
	}

	sendData := [][]byte{[]byte("SEND"), []byte(md5s), []byte(fmt.Sprintf("%d", fileSize))}

	var buf bytes.Buffer
	for _, value := range sendData {
		buf.Write(value)
		buf.Write(separatorBytes)
	}
	buf.Write(newLineBytes)

	return p.sendData(client, buf.Bytes())
}