Code refactoring practice

Time:2021-11-25

background

In the user avatar upload function, the server upload process is as follows:

  1. base64Decode to string
  2. Write pictures to the local temporary directory of the server
  3. Upload toAlibaba cloud OSS/Qiniu cloud storage
  4. Picture review

The code is as follows

type Parms struct {
    Head string // the client sends a Base64 string
}
func Upload(p Params){
    if p.Head == ""{
        return errors.New("img is empty")
    }
    
    str,err := Base64Decode(p.Head)
    if err != nil{
        return err
    }
    
    filename := makeUniqueName()
    if err := FileWrite(filename,str);err != nil{
         return err
    }
    
    err := UploadAliOss(filename)
    if err != nil{
        return err
    }
    
    err := VerifyImg(filename)
    if err != nil{
        return err
    }

}

problem

Previous projects passedjenkinsIssue different online instructions. For each new project, the operation and maintenance needs to write a separate setshellCommands cannot be reused, and the maintenance cost is relatively high. Later, we upgraded the deployment method. We only need to modify the project deployment directory configuration file, which will be described later.

It has been running stably all the time. There are problems in the feedback received at the weekend, and users can’t upload avatars.

  • Check the of the codegitHistory submission record, no code has been modified recently, exclusion codebug
  • The error message is only available when callingFileWriteandUploadAliOssOccurs when two methods are used
  • When packaging the image, no temporary directory was created in the container, so the file cannot be written and the user’s Avatar cannot be uploaded

Temporary settlement

Manually create a temporary directory in the container to save the picture files.

Completely solve

In my opinion, all picture files are saved in the third-party object storage, and it is completely unnecessary for the server to save one, which will only occupy disk space.

We can createReaderObject reads the picture file into the buffer, and then uploads it to the third-party object storage.

The code is as follows

func Upload(p Params){

    if p.Head == ""{
        return errors.New("img is empty")
    }
    
    str,err := Base64Decode(p.Head)
    if err != nil{
        return err
    }
    
    filename := makeUniqueName()
    buf := bytes.NewReader([]byte(str))
    
    err := UploadAliOssBuff(filename,buf)
    if err != nil{
        return err
    }
    
    err := VerifyImg(filename)
    if err != nil{
        return err
    }

}

summary

When taking over the maintenance of old projects, it is inevitable to encounter pits. In addition to the bad writing done by Tucao, we can do everything we can to improve the quality of code maintenance and make complaints about later maintainers easier.

An open space, there may be only a few pieces of garbage at first. If you don’t clean up all the time, you throw it away, and he will throw it away, and it will soon become a garbage dump.

WeChat official account

Code refactoring practice