2020-05-26 22:07:55 +02:00
|
|
|
// Vikunja is a to-do list application to facilitate your life.
|
|
|
|
// Copyright 2018-2020 Vikunja and contributors. All rights reserved.
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package unsplash
|
|
|
|
|
|
|
|
import (
|
2020-05-29 22:11:49 +02:00
|
|
|
"bytes"
|
2020-10-11 22:10:03 +02:00
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2020-12-23 16:32:28 +01:00
|
|
|
"xorm.io/xorm"
|
|
|
|
|
2020-05-26 22:07:55 +02:00
|
|
|
"code.vikunja.io/api/pkg/config"
|
|
|
|
"code.vikunja.io/api/pkg/files"
|
|
|
|
"code.vikunja.io/api/pkg/log"
|
|
|
|
"code.vikunja.io/api/pkg/models"
|
|
|
|
"code.vikunja.io/api/pkg/modules/background"
|
2020-10-10 18:53:59 +02:00
|
|
|
"code.vikunja.io/api/pkg/modules/keyvalue"
|
|
|
|
e "code.vikunja.io/api/pkg/modules/keyvalue/error"
|
2020-05-26 22:07:55 +02:00
|
|
|
"code.vikunja.io/web"
|
|
|
|
)
|
|
|
|
|
2020-10-10 18:53:59 +02:00
|
|
|
const (
|
|
|
|
unsplashAPIURL = `https://api.unsplash.com/`
|
|
|
|
cachePrefix = `unsplash_photo_`
|
|
|
|
)
|
2020-05-31 22:06:59 +02:00
|
|
|
|
2020-05-26 22:07:55 +02:00
|
|
|
// Provider represents an unsplash image provider
|
|
|
|
type Provider struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
// SearchResult is a search result from unsplash's api
|
|
|
|
type SearchResult struct {
|
|
|
|
Total int `json:"total"`
|
|
|
|
TotalPages int `json:"total_pages"`
|
|
|
|
Results []*Photo `json:"results"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Photo represents an unpslash photo as returned by their api
|
|
|
|
type Photo struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
CreatedAt string `json:"created_at"`
|
|
|
|
Width int `json:"width"`
|
|
|
|
Height int `json:"height"`
|
|
|
|
Color string `json:"color"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
User struct {
|
|
|
|
Username string `json:"username"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
} `json:"user"`
|
|
|
|
Urls struct {
|
|
|
|
Raw string `json:"raw"`
|
|
|
|
Full string `json:"full"`
|
|
|
|
Regular string `json:"regular"`
|
|
|
|
Small string `json:"small"`
|
|
|
|
Thumb string `json:"thumb"`
|
|
|
|
} `json:"urls"`
|
|
|
|
Links struct {
|
2020-05-31 22:06:59 +02:00
|
|
|
Self string `json:"self"`
|
|
|
|
HTML string `json:"html"`
|
|
|
|
Download string `json:"download"`
|
|
|
|
DownloadLocation string `json:"download_location"`
|
2020-05-26 22:07:55 +02:00
|
|
|
} `json:"links"`
|
|
|
|
}
|
|
|
|
|
2020-05-31 17:59:39 +02:00
|
|
|
// We're caching the initial collection to save a few api requests as this is retrieved every time a
|
|
|
|
// user opens the settings page.
|
|
|
|
type initialCollection struct {
|
|
|
|
lastCached time.Time
|
2020-05-31 18:09:46 +02:00
|
|
|
// images contains a slice of images by page they belong to
|
|
|
|
// this allows us to cache individual pages.
|
|
|
|
images map[int64][]*background.Image
|
2020-05-31 17:59:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var emptySearchResult *initialCollection
|
|
|
|
|
2020-05-31 22:06:59 +02:00
|
|
|
func doGet(url string, result ...interface{}) (err error) {
|
2020-10-11 22:10:03 +02:00
|
|
|
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, unsplashAPIURL+url, nil)
|
2020-05-26 22:07:55 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Add("Authorization", "Client-ID "+config.BackgroundsUnsplashAccessToken.GetString())
|
|
|
|
hc := http.Client{}
|
|
|
|
resp, err := hc.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-10-11 22:10:03 +02:00
|
|
|
defer resp.Body.Close()
|
2020-05-26 22:07:55 +02:00
|
|
|
|
2020-05-31 22:06:59 +02:00
|
|
|
if len(result) > 0 {
|
|
|
|
return json.NewDecoder(resp.Body).Decode(result[0])
|
|
|
|
}
|
|
|
|
|
2020-05-26 22:07:55 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-29 15:33:46 +02:00
|
|
|
func getImageID(fullURL string) string {
|
|
|
|
// Unsplash image urls have the form
|
|
|
|
// https://images.unsplash.com/photo-1590622878565-c662a7fd1394?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=200&fit=max&ixid=eyJhcHBfaWQiOjcyODAwfQ
|
|
|
|
// We only need the "photo-*" part of it.
|
2020-05-31 22:40:27 +02:00
|
|
|
return strings.Replace(strings.Split(fullURL, "?")[0], "https://images.unsplash.com/", "", 1)
|
2020-05-29 15:33:46 +02:00
|
|
|
}
|
|
|
|
|
2020-05-31 22:06:59 +02:00
|
|
|
// Gets an unsplash photo either from cache or directly from the unsplash api
|
|
|
|
func getUnsplashPhotoInfoByID(photoID string) (photo *Photo, err error) {
|
2020-10-10 18:53:59 +02:00
|
|
|
|
|
|
|
p, err := keyvalue.Get(cachePrefix + photoID)
|
|
|
|
if err != nil && !e.IsErrValueNotFoundForKey(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil && e.IsErrValueNotFoundForKey(err) {
|
2020-05-31 22:06:59 +02:00
|
|
|
log.Debugf("Image information for unsplash photo %s not cached, requesting from unsplash...", photoID)
|
|
|
|
photo = &Photo{}
|
|
|
|
err = doGet("photos/"+photoID, photo)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-10-10 18:53:59 +02:00
|
|
|
} else {
|
|
|
|
photo = p.(*Photo)
|
2020-05-31 22:06:59 +02:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-26 22:07:55 +02:00
|
|
|
// Search is the implementation to search on unsplash
|
|
|
|
// @Summary Search for a background from unsplash
|
|
|
|
// @Description Search for a list background from unsplash
|
|
|
|
// @tags list
|
|
|
|
// @Produce json
|
|
|
|
// @Security JWTKeyAuth
|
|
|
|
// @Param s query string false "Search backgrounds from unsplash with this search term."
|
|
|
|
// @Param p query int false "The page number. Used for pagination. If not provided, the first page of results is returned."
|
|
|
|
// @Success 200 {array} background.Image "An array with photos"
|
|
|
|
// @Failure 500 {object} models.Message "Internal error"
|
|
|
|
// @Router /backgrounds/unsplash/search [get]
|
2020-12-23 16:32:28 +01:00
|
|
|
func (p *Provider) Search(s *xorm.Session, search string, page int64) (result []*background.Image, err error) {
|
2020-05-26 22:07:55 +02:00
|
|
|
|
|
|
|
// If we don't have a search query, return results from the unsplash featured collection
|
|
|
|
if search == "" {
|
2020-05-31 17:59:39 +02:00
|
|
|
|
2020-05-31 18:09:46 +02:00
|
|
|
var existsForPage bool
|
|
|
|
|
|
|
|
if emptySearchResult != nil &&
|
2020-05-31 20:50:51 +02:00
|
|
|
time.Since(emptySearchResult.lastCached) < time.Hour {
|
2020-05-31 18:09:46 +02:00
|
|
|
_, existsForPage = emptySearchResult.images[page]
|
|
|
|
}
|
|
|
|
|
|
|
|
if existsForPage {
|
2020-05-31 18:12:32 +02:00
|
|
|
log.Debugf("Serving initial unsplash collection for page %d from cache, last updated at %v", page, emptySearchResult.lastCached)
|
2020-05-31 18:09:46 +02:00
|
|
|
return emptySearchResult.images[page], nil
|
2020-05-31 17:59:39 +02:00
|
|
|
}
|
|
|
|
|
2020-05-31 18:09:46 +02:00
|
|
|
log.Debugf("Retrieving initial unsplash collection for page %d from unsplash api", page)
|
2020-05-31 17:59:39 +02:00
|
|
|
|
2020-05-26 22:07:55 +02:00
|
|
|
collectionResult := []*Photo{}
|
|
|
|
err = doGet("collections/317099/photos?page="+strconv.FormatInt(page, 10)+"&per_page=25&order_by=latest", &collectionResult)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
result = []*background.Image{}
|
|
|
|
for _, p := range collectionResult {
|
|
|
|
result = append(result, &background.Image{
|
2020-05-29 15:33:46 +02:00
|
|
|
ID: p.ID,
|
|
|
|
URL: getImageID(p.Urls.Raw),
|
2020-05-26 22:07:55 +02:00
|
|
|
Info: &models.UnsplashPhoto{
|
|
|
|
UnsplashID: p.ID,
|
|
|
|
Author: p.User.Username,
|
|
|
|
AuthorName: p.User.Name,
|
|
|
|
},
|
|
|
|
})
|
2020-10-10 18:53:59 +02:00
|
|
|
if err := keyvalue.Put(cachePrefix+p.ID, p); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-26 22:07:55 +02:00
|
|
|
}
|
2020-05-31 17:59:39 +02:00
|
|
|
|
2020-05-31 18:09:46 +02:00
|
|
|
// Put the collection in cache
|
|
|
|
if emptySearchResult == nil {
|
|
|
|
emptySearchResult = &initialCollection{
|
|
|
|
images: make(map[int64][]*background.Image),
|
|
|
|
}
|
2020-05-31 17:59:39 +02:00
|
|
|
}
|
2020-05-31 18:09:46 +02:00
|
|
|
|
|
|
|
emptySearchResult.lastCached = time.Now()
|
|
|
|
emptySearchResult.images[page] = result
|
|
|
|
|
2020-05-26 22:07:55 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
searchResult := &SearchResult{}
|
2020-06-26 00:37:10 +02:00
|
|
|
err = doGet("search/photos?query="+url.QueryEscape(search)+"&page="+strconv.FormatInt(page, 10)+"&per_page=25", &searchResult)
|
2020-05-26 22:07:55 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
result = []*background.Image{}
|
|
|
|
for _, p := range searchResult.Results {
|
|
|
|
result = append(result, &background.Image{
|
2020-05-29 15:33:46 +02:00
|
|
|
ID: p.ID,
|
|
|
|
URL: getImageID(p.Urls.Raw),
|
2020-05-27 22:12:12 +02:00
|
|
|
Info: &models.UnsplashPhoto{
|
|
|
|
UnsplashID: p.ID,
|
|
|
|
Author: p.User.Username,
|
|
|
|
AuthorName: p.User.Name,
|
|
|
|
},
|
2020-05-26 22:07:55 +02:00
|
|
|
})
|
2020-10-10 18:53:59 +02:00
|
|
|
if err := keyvalue.Put(cachePrefix+p.ID, p); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-26 22:07:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set sets an unsplash photo as list background
|
|
|
|
// @Summary Set an unsplash photo as list background
|
2020-06-11 19:31:37 +02:00
|
|
|
// @Description Sets a photo from unsplash as list background.
|
2020-05-26 22:07:55 +02:00
|
|
|
// @tags list
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Security JWTKeyAuth
|
|
|
|
// @Param id path int true "List ID"
|
|
|
|
// @Param list body background.Image true "The image you want to set as background"
|
|
|
|
// @Success 200 {object} models.List "The background has been successfully set."
|
2020-06-28 16:25:46 +02:00
|
|
|
// @Failure 400 {object} web.HTTPError "Invalid image object provided."
|
|
|
|
// @Failure 403 {object} web.HTTPError "The user does not have access to the list"
|
2020-05-26 22:07:55 +02:00
|
|
|
// @Failure 500 {object} models.Message "Internal error"
|
|
|
|
// @Router /lists/{id}/backgrounds/unsplash [post]
|
2020-12-23 16:32:28 +01:00
|
|
|
func (p *Provider) Set(s *xorm.Session, image *background.Image, list *models.List, auth web.Auth) (err error) {
|
2020-05-26 22:07:55 +02:00
|
|
|
|
|
|
|
// Find the photo
|
2020-05-31 22:06:59 +02:00
|
|
|
photo, err := getUnsplashPhotoInfoByID(image.ID)
|
|
|
|
if err != nil {
|
|
|
|
return
|
2020-05-26 22:07:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Download the photo from unsplash
|
|
|
|
// The parameters crop the image to a max width of 2560 and a max height of 2048 to save bandwidth and storage.
|
2020-10-11 22:10:03 +02:00
|
|
|
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, photo.Urls.Raw+"&w=2560&h=2048&q=90", nil)
|
2020-05-26 22:07:55 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-10-11 22:10:03 +02:00
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-26 22:07:55 +02:00
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2020-05-29 22:11:49 +02:00
|
|
|
if resp.StatusCode > 399 {
|
|
|
|
b := bytes.Buffer{}
|
|
|
|
_, _ = b.ReadFrom(resp.Body)
|
|
|
|
log.Errorf("Error getting unsplash photo %s: Request failed with status %d, message was %s", photo.ID, resp.StatusCode, b.String())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-31 22:06:59 +02:00
|
|
|
log.Debugf("Downloaded unsplash photo %s", image.ID)
|
|
|
|
|
|
|
|
// Ping the unsplash download endpoint (again, unsplash api guidelines)
|
|
|
|
err = doGet(strings.Replace(photo.Links.DownloadLocation, unsplashAPIURL, "", 1))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Debugf("Pinged unsplash download endpoint for photo %s", image.ID)
|
2020-05-26 22:07:55 +02:00
|
|
|
|
|
|
|
// Save it as a file in vikunja
|
|
|
|
file, err := files.Create(resp.Body, "", 0, auth)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the old background if one exists
|
|
|
|
if list.BackgroundFileID != 0 {
|
|
|
|
file := files.File{ID: list.BackgroundFileID}
|
|
|
|
if err := file.Delete(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-23 16:32:28 +01:00
|
|
|
if err := models.RemoveUnsplashPhoto(s, list.BackgroundFileID); err != nil {
|
2020-05-26 22:07:55 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save the relation that we got it from unsplash
|
|
|
|
unsplashPhoto := &models.UnsplashPhoto{
|
|
|
|
FileID: file.ID,
|
|
|
|
UnsplashID: image.ID,
|
|
|
|
Author: photo.User.Username,
|
|
|
|
AuthorName: photo.User.Name,
|
|
|
|
}
|
2020-12-23 16:32:28 +01:00
|
|
|
err = unsplashPhoto.Save(s)
|
2020-05-26 22:07:55 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Debugf("Saved unsplash photo %s as file %d with new entry %d", image.ID, file.ID, unsplashPhoto.ID)
|
|
|
|
|
|
|
|
// Set the file in the list
|
|
|
|
list.BackgroundFileID = file.ID
|
|
|
|
list.BackgroundInformation = unsplashPhoto
|
|
|
|
|
|
|
|
// Set it as the list background
|
2020-12-23 16:32:28 +01:00
|
|
|
return models.SetListBackground(s, list.ID, file)
|
2020-05-26 22:07:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Pingback pings the unsplash api if an unsplash photo has been accessed.
|
2020-12-23 16:32:28 +01:00
|
|
|
func Pingback(s *xorm.Session, f *files.File) {
|
2020-05-26 22:07:55 +02:00
|
|
|
// Check if the file is actually downloaded from unsplash
|
2020-12-23 16:32:28 +01:00
|
|
|
unsplashPhoto, err := models.GetUnsplashPhotoByFileID(s, f.ID)
|
2020-05-26 22:07:55 +02:00
|
|
|
if err != nil {
|
2020-06-11 19:31:37 +02:00
|
|
|
if files.IsErrFileIsNotUnsplashFile(err) {
|
|
|
|
return
|
|
|
|
}
|
2020-05-26 22:07:55 +02:00
|
|
|
log.Errorf("Unsplash Pingback: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do the ping
|
2020-05-31 22:06:59 +02:00
|
|
|
pingbackByPhotoID(unsplashPhoto.UnsplashID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func pingbackByPhotoID(photoID string) {
|
2020-10-11 22:10:03 +02:00
|
|
|
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "https://views.unsplash.com/v?app_id="+config.BackgroundsUnsplashApplicationID.GetString()+"&photo_id="+photoID, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Unsplash Pingback Failed: %s", err.Error())
|
|
|
|
}
|
|
|
|
_, err = http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
2020-05-26 22:07:55 +02:00
|
|
|
log.Errorf("Unsplash Pingback Failed: %s", err.Error())
|
|
|
|
}
|
2020-05-31 22:06:59 +02:00
|
|
|
log.Debugf("Pinged unsplash for photo %s", photoID)
|
2020-05-26 22:07:55 +02:00
|
|
|
}
|