...

Source file src/gitlab.com/tslocum/sriracha/sriracha.go

Documentation: gitlab.com/tslocum/sriracha

     1  package sriracha
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"io"
     7  )
     8  
     9  type UseExtension struct {
    10  	Extension Extension
    11  	FileTypes []string
    12  	Events    []string
    13  }
    14  
    15  type Options struct {
    16  	Extensions []UseExtension
    17  }
    18  
    19  // sriracha represents an imageboard system.
    20  type sriracha struct {
    21  	boards     map[string]*Board
    22  	extensions []UseExtension
    23  
    24  	newPost chan *Post
    25  }
    26  
    27  func (s *sriracha) handleAttachment(file io.ReadSeeker, size int64, mime string) (*Attachment, error) {
    28  	if mime == "" {
    29  		return nil, errors.New("unsupported file type")
    30  	}
    31  
    32  	for _, ext := range s.extensions {
    33  		for _, fileType := range ext.FileTypes {
    34  			if fileType == mime {
    35  				_, err := file.Seek(0, io.SeekStart)
    36  				if err != nil {
    37  					return nil, errors.New("failed to seek to start of file")
    38  				}
    39  
    40  				attachment, err := ext.Extension.Attach(file, size, mime)
    41  				if err != nil {
    42  					return nil, err
    43  				} else if attachment != nil {
    44  					return attachment, nil
    45  				}
    46  			}
    47  		}
    48  	}
    49  
    50  	return nil, fmt.Errorf("unsupported file type: %s", mime)
    51  }
    52  
    53  func (s *sriracha) handlePosts() {
    54  	for post := range s.newPost {
    55  		for _, ext := range s.extensions {
    56  			ext.Extension.Post() // TODO
    57  			_ = post
    58  		}
    59  	}
    60  }
    61  
    62  // Run initializes and runs a sriracha imageboard system.
    63  func Run(options *Options) error {
    64  	s := &sriracha{
    65  		boards:     make(map[string]*Board),
    66  		extensions: options.Extensions,
    67  		newPost:    make(chan *Post, 10),
    68  	}
    69  
    70  	done := make(chan error)
    71  	go func() {
    72  		done <- s.runWebServer()
    73  	}()
    74  
    75  	go s.handlePosts()
    76  
    77  	return <-done
    78  }
    79  

View as plain text