summaryrefslogtreecommitdiff
path: root/app/relay/system.js
blob: e8feee839f6b019d1318ae25eec593c0ed62a98f (plain)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import { execFile } from 'child_process'
import * as path from 'path'
import * as fs from 'fs'
import readdir from 'fs-readdir-promise'
import sharp from 'sharp'

import modules from './modules'
import * as runner from './runner'

const MEGABYTES = 1024 * 1024
const MAX_TRANSFER_SIZE = 2.5 * MEGABYTES
const MAX_LOAD_SIZE = 108 * MEGABYTES
const THUMBNAIL_SIZE = 200
const THUMBNAIL_QUALITY = 80

function sanitize_path(f){
  return f.replace(/^\//,'').replace(/\.\./, '')
}

export function upload_file(task, cb) {
  const module = modules[task.module]
  const filepath = path.join(module.cwd, sanitize_path(task.path), sanitize_path(task.filename))
  const params = [
    '-F', 'module=' + task.module,
    '-F', 'activity=' + task.activity,
    '-F', 'generated=' + (String(task.generated) === 'true'),
    '-F', 'processed=' + (String(task.processed) === 'true'),
    '-F', "file=@" + filepath,
    process.env.API_REMOTE + '/api/folder/' + task.folder_id + '/upload/',
  ]
  console.log(params)
  execFile('curl', params, cb)
  // curl \
  // -F "module=samplernn" \
  // -F "activity=train" \
  // -F "file=@woods1.jpg" \
  // localhost:7013/api/folder/1/upload/
}

export function run_system_command(opt, cb) {
  console.log('running system command:', opt.cmd)
  switch(opt.cmd) {
    case 'nvidia-smi':
    case 'uptime':
    case 'w':
      execFile(opt.cmd, [], cb)
      break
    case 'ps':
      execFile('ps', ['au'], cb)
      break
    case 'df':
      execFile('df', ['-h'], cb)
      break
    case 'ls':
      list_directory(opt, cb)
      break
    case 'du':
      disk_usage(opt, cb)
      break
    case 'list_sequences':
      list_sequences(opt, cb)
      break
    case 'dir_to_video':
      dir_to_video(opt, cb)
      break
    default:
      cb({ error: 'no such command' })
      break
  }
}

export function module_dir(opt, dir){
  if (!opt.module || ! modules[opt.module]) {
    return null
  }
  const module = modules[opt.module]
  if (!module) {
    return null
  }
  return path.join(module.cwd, dir.replace(/\.\.?\//g, ''))
}

export function read_file(opt, cb) {
  const fn = module_dir(opt, opt.fn)
  if (!fn) return cb([])
  stat_promise(fn).then(stat => {
    if (stat.size > MAX_TRANSFER_SIZE) {
      return cb({ error: 'file too large'})
    }
    fs.readFile(fn, (err, buf) => cb({
      error: err,
      name: opt.fn,
      path: fn,
      date: stat.ctime,
      size: stat.size,
      buf
    }))
  }).catch(() => cb({ error: 'error reading file' }))
}

export function thumbnail(opt, cb) {
  const fn = module_dir(opt, opt.fn)
  if (!fn) return cb([])
  stat_promise(fn).then(stat => {
    if (stat.size > MAX_LOAD_SIZE) {
      return cb({ error: 'file too large'})
    }
    sharp(fn)
      .resize(opt.size || THUMBNAIL_SIZE)
      .jpeg({ quality: opt.quality || THUMBNAIL_QUALITY })
      .toBuffer()
      .then(buf => cb({
        name: opt.fn,
        path: fn,
        date: stat.ctime,
        size: stat.size,
        buf
      }))
      .catch(err => cb({
        error: err,
        name: opt.fn,
        path: fn,
        date: stat.ctime,
        size: stat.size,
        buf: null,
      }))
  })
}

export function list_directory(opt, cb) {
  const dir = module_dir(opt, opt.dir)
  if (!dir) return cb([])
  fs.readdir(dir, (err, files) => {
    const statPromises = (files || []).filter(f => f[0] !== '.').map(f => {
      return new Promise((resolve, reject) => {
        const full_path = path.join(dir, f)
        fs.stat(full_path, (err, stat={}) => {
          resolve({
            name: f,
            date: stat.ctime,
            size: stat.size,
            dir: stat.isDirectory ? stat.isDirectory() : false,
          })
        })
      })
    })
    Promise.all(statPromises).then(stats => {
      cb(stats, dir)
    }).catch(error => {
      cb(error)
    })
  })
}

export function count_directory(opt, cb) {
  const dir = module_dir(opt, opt.dir)
  if (!dir) return cb([])
  fs.readdir(dir, (err, files) => {
    err ? cb(err) : cb(files.length)
  })
}

// list the contents of a directory of sequences
export function list_sequences(opt, cb) {
  list_directory(opt, (files, root_dir) => {
    // console.log(files, root_dir)
    const sequencePromises = files.filter(d => !!d.dir).map(f => {
      return list_sequence(opt, f, root_dir)
    })
    Promise.all(sequencePromises).then(cb).catch(error => {
      console.error(error)
      cb([])
    })
  })
}
export function list_sequence(opt, f, root_dir) {
  return new Promise( (resolve, reject) => {
    let sequence = {
      name: f.name,
      date: f.date,
      size: f.size,
      frame: null,
      count: 0,
    }
    readdir(path.join(root_dir, f.name)).then(files => {
      if (! files.length) {
        return resolve(sequence)
      }
      const middle_file = files[Math.floor(files.length/2)]
      if (! middle_file) return resolve(sequence)
      sequence.frame = {
        prefix: middle_file.split('_')[0],
      }
      sequence.count = files.length
      // console.log(sequence.count)
      return stat_promise(path.join(root_dir, f.name, middle_file))
    }).then(stat => {
      sequence.frame.date = stat.date
      sequence.frame.size = stat.size
      resolve(sequence)
    }).catch(err => reject(err))
  })
}

export function stat_promise(fn) {
  return new Promise((resolve, reject) => {
    fs.stat(fn, (err, stat={}) => {
      if (err) reject(err)
      resolve(stat)
    })
  })
}
export function dir_to_video(opt, db) {
  const dir = module_dir(opt, opt.dir)
  if (!dir) return cb([])
  // input: the path (in results/) you want as a video
  // output: the path (in renders/) that contains the video
  // run the dir to video script with CWD as the directory and first input as ../renders plus the directory name
  // list the file in renders...
  execFile('./bin/dir_to_video.pl', params, {
    cwd: module.cwd,
  }, cb)
}


export function disk_usage(opt, cb) {
  const dir = module_dir(opt, opt.dir)
  if (!dir) return cb([])
  execFile('du', ['-d', 1, dir], cb)
}

export function run_script(task, cb) {
  if (!task.module || ! modules[task.module]) {
    cb("")
  }
  const module = modules[task.module]
  const activity = module.activities[task.activity]
  const { interpreter, params, cancelled } = runner.build_params(module, activity, task)
  if (cancelled) return { type: 'error', error: "Task builder cancelled process" }
  if (! interpreter) return { type: 'error', error: "No such interpreter: " + activity.interpreter }
  if (! activity.isScript) return { type: 'error', error: "Not a script: " + task.module }

  console.log('running task', task.activity)
  console.log(module.cwd)
  console.log(interpreter.cmd, params)
  execFile(interpreter.cmd, params, {
    cwd: module.cwd,
  }, cb)
}