public.ts 1.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import RepositoryFile from 'model/repository/RepositoryFile';
import RepositoryImage from 'model/repository/RepositoryImage';

/**
 * Pick a random CDN server if available
 */
function getCDNServer(getState) {
  const apiInfo = getState().serverInfo.data;
  if (apiInfo && apiInfo.cdnServers && apiInfo.cdnServers.length > 0) {
    const cdnServers = apiInfo.cdnServers;
    return cdnServers[Math.round((cdnServers.length - 1) * Math.random())] + '/repository/d';
  } else {
    return '/proxy/uploads/d';
  }
}

/**
 * Renders CDN-based URL for public files, API URL for protected files
 *
 * @param file RepositoryFile for which to render URL
 */
export const repositoryDownloadUrl = (file: RepositoryFile) => (dispatch, getState) => {
  if (! file) {
    return null;
  }
  if (file._permissions && file._permissions.isPublic) {
27
    return getCDNServer(getState) + `${file.storagePath}`;
28
  } else {
29
    return '/proxy/uploads/d' + `${file.storagePath}`;
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
  }
};


/**
 * Renders CDN-based URL for public images, API URL for protected images
 *
 * @param image RepositoryImage for which to render thumbnail URL
 */
export const repositoryThumbnailUrl = (image: RepositoryImage, width: number, height: number) => (dispatch, getState) => {
  if (! image) {
    return null;
  }
  if (image._permissions && image._permissions.isPublic) {
    return getCDNServer(getState) + `/_thumbs${image.thumbnailPath}/${width}x${height}.png`;
  } else {
    return '/proxy/uploads/d' + `/_thumbs${image.thumbnailPath}/${width}x${height}.png`;
  }
};