File size: 3,099 Bytes
0070fce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import type { FileNodeInfo } from '@/api/files'
import { apiBase } from '@/api'
import { uniqBy } from 'lodash-es'
import { isTauri } from './env'

const encode = encodeURIComponent
export const toRawFileUrl = (file: FileNodeInfo, download = false) =>
  `${apiBase.value}/file?path=${encode(file.fullpath)}&t=${encode(file.date)}${download ? `&disposition=${encode(file.name)}` : ''
  }`
export const toImageThumbnailUrl = (file: FileNodeInfo, size: string = '512x512') => {
  return `${apiBase.value}/image-thumbnail?path=${encode(file.fullpath)}&size=${size}&t=${encode(
    file.date
  )}`
}

export const toStreamVideoUrl = (file: FileNodeInfo) =>
  `${apiBase.value}/stream_video?path=${encode(file.fullpath)}`

export const toVideoCoverUrl = (file: FileNodeInfo) =>
  (isTauri ? '' : parent.document.location.origin) + `${apiBase.value}/video_cover?path=${encode(file.fullpath)}&mt=${encode(file.date)}`

export type FileTransferData = {
  path: string[]
  loc: string
  includeDir: boolean
  nodes: FileNodeInfo[]
  __id: 'FileTransferData'
}

export const isFileTransferData = (v: any): v is FileTransferData =>
  typeof v === 'object' && v.__id === 'FileTransferData'

export const getFileTransferDataFromDragEvent = (e: DragEvent) => {
  const data = JSON.parse(e.dataTransfer?.getData('text') ?? '{}')
  return isFileTransferData(data) ? data : null
}

export const uniqueFile = (files: FileNodeInfo[]) => uniqBy(files, 'fullpath')

export function isImageFile (filename: string): boolean {
  if (typeof filename !== 'string') {
    return false
  }
  const exts = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp', '.avif', '.jpe']
  const extension = filename.split('.').pop()?.toLowerCase()
  return extension !== undefined && exts.includes(`.${extension}`)
}

export function isVideoFile (filename: string): boolean {
  if (typeof filename !== 'string') {
    return false
  }
  const exts = ['.mp4', '.m4v', '.avi', '.mkv', '.mov', '.wmv', '.flv', '.ts']
  const extension = filename.split('.').pop()?.toLowerCase()
  return extension !== undefined && exts.includes(`.${extension}`)
}

export const isMediaFile = (file: string) => isImageFile(file) || isVideoFile(file)

export function downloadFiles (urls: string[]) {
  const link = document.createElement('a')
  link.style.display = 'none'
  document.body.appendChild(link)

  urls.forEach((url) => {
    const urlObject = new URL(url, 'https://github.com/zanllp/sd-webui-infinite-image-browsing')
    let filename = ''
    const disposition = urlObject.searchParams.get('disposition')
    if (disposition) {
      filename = disposition
    }
    link.href = url
    link.download = filename
    link.click()
  })

  document.body.removeChild(link)
}

export const downloadFileInfoJSON = (files: FileNodeInfo[], name?: string) => {
  const url = window.URL.createObjectURL(new Blob([JSON.stringify({
    files
  }, null, 4)]))
  const link = document.createElement('a')
  link.href = url
  link.setAttribute('download', `iib_imginfo_${name ?? new Date().toLocaleString()}.json`)
  document.body.appendChild(link)
  link.click()
}