func (s *Slack) handleFiles(action string, params map[string]string) (interface{}, error) { var v interface{} var err error switch action { case "delete": err = s.s.DeleteFile(params["file"]) case "info": count := getIntParam(params, "count", slack.DEFAULT_FILES_COUNT) page := getIntParam(params, "page", slack.DEFAULT_FILES_PAGE) files, comments, pages, err := s.s.GetFileInfo(params["file"], count, page) if err != nil { return nil, err } v = map[string]interface{}{ "file": files, "comments": comments, "paging": pages, } case "list": listParam := slack.GetFilesParameters{} listParam.UserId = getStringParam(params, "user", slack.DEFAULT_FILES_USERID) listParam.TimestampFrom = slack.JSONTime(getIntParam(params, "ts_from", slack.DEFAULT_FILES_TS_FROM)) listParam.TimestampTo = slack.JSONTime(getIntParam(params, "ts_to", slack.DEFAULT_FILES_TS_TO)) listParam.Types = getStringParam(params, "types", slack.DEFAULT_FILES_TYPES) listParam.Count = getIntParam(params, "count", slack.DEFAULT_FILES_COUNT) listParam.Page = getIntParam(params, "page", slack.DEFAULT_FILES_PAGE) files, pages, err := s.s.GetFiles(listParam) if err != nil { return nil, err } v = map[string]interface{}{ "files": files, "paging": pages, } case "upload": uploadParams := slack.FileUploadParameters{} uploadParams.File = getStringParam(params, "file", "") uploadParams.Content = getStringParam(params, "content", "") uploadParams.Filetype = getStringParam(params, "filetype", "") uploadParams.Filename = getStringParam(params, "filename", "") uploadParams.Title = getStringParam(params, "title", "") uploadParams.InitialComment = getStringParam(params, "initial_comment", "") channels := getStringParam(params, "channels", "") uploadParams.Channels = strings.Split(channels, ",") file, err := s.s.UploadFile(uploadParams) if err != nil { return nil, err } v = map[string]interface{}{ "file": file, } default: return nil, fmt.Errorf("invalid files action %s", action) } return v, err }
func (s *Slack) getFileList(date string) ([]slack.File, error) { loc := time.FixedZone("Asia/Tokyo", 9*60*60) timeTo, err := time.ParseInLocation("20060102", date[0:8], loc) if err != nil { return nil, err } var fileArray []slack.File nDaysAgo := time.Duration(-24 * s.NDaysAgo) timeFrom := timeTo.Add(nDaysAgo * time.Hour) page := 1 for { params := slack.GetFilesParameters{ User: s.UserId, TimestampFrom: slack.JSONTime(int(timeFrom.Unix())), TimestampTo: slack.JSONTime(int(timeTo.Unix())), Types: "GIF", Page: page, } files, p, err := s.Api.GetFiles(params) if err != nil { return nil, err } if len(files) == 0 { break } else { fileArray = append(fileArray, files...) } if p.Page >= p.Pages { break } else { page += 1 } } return fileArray, nil }
func unixFromTimestamp(ts string) slack.JSONTime { i, _ := strconv.Atoi(strings.Split(ts, ".")[0]) return slack.JSONTime(i) }