util.go (1721B)
1 // GoToSocial 2 // Copyright (C) GoToSocial Authors admin@gotosocial.org 3 // SPDX-License-Identifier: AGPL-3.0-or-later 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Affero General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Affero General Public License for more details. 14 // 15 // You should have received a copy of the GNU Affero General Public License 16 // along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 package media 19 20 import ( 21 "fmt" 22 "strconv" 23 "strings" 24 ) 25 26 func parseFocus(focus string) (focusx, focusy float32, err error) { 27 if focus == "" { 28 return 29 } 30 spl := strings.Split(focus, ",") 31 if len(spl) != 2 { 32 err = fmt.Errorf("improperly formatted focus %s", focus) 33 return 34 } 35 xStr := spl[0] 36 yStr := spl[1] 37 if xStr == "" || yStr == "" { 38 err = fmt.Errorf("improperly formatted focus %s", focus) 39 return 40 } 41 fx, err := strconv.ParseFloat(xStr, 32) 42 if err != nil { 43 err = fmt.Errorf("improperly formatted focus %s: %s", focus, err) 44 return 45 } 46 if fx > 1 || fx < -1 { 47 err = fmt.Errorf("improperly formatted focus %s", focus) 48 return 49 } 50 focusx = float32(fx) 51 fy, err := strconv.ParseFloat(yStr, 32) 52 if err != nil { 53 err = fmt.Errorf("improperly formatted focus %s: %s", focus, err) 54 return 55 } 56 if fy > 1 || fy < -1 { 57 err = fmt.Errorf("improperly formatted focus %s", focus) 58 return 59 } 60 focusy = float32(fy) 61 return 62 }