summaryrefslogtreecommitdiff
path: root/core/modules/download.sh
blob: a71fcc4560e44609881a061c528df68df11d60cb (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
#!/usr/bin/env bash
# Module: download
# Description: Descarga ficheros remotos con soporte para reintentos, proxy y reanudación
# Author: Luis GuLo
# Version: 1.1.0
# Dependencies: wget o curl, sudo (si become=true)

download_task() {
  local host="$1"; shift
  declare -A args
  for arg in "$@"; do key="${arg%%=*}"; value="${arg#*=}"; args["$key"]="$value"; done

  local url="${args[url]}"
  local dest="${args[dest]:-$(basename "$url")}"
  local proxy="${args[proxy]:-}"
  local continue="${args[continue]:-true}"
  local become="${args[become]}"
  local prefix=""
  [ "$become" = "true" ] && prefix="sudo"

  # 🌐 Cargar traducciones
  local lang="${shflow_vars[language]:-es}"
  local trfile="$(dirname "${BASH_SOURCE[0]}")/download.tr.${lang}"
  declare -A tr
  if [[ -f "$trfile" ]]; then while IFS='=' read -r k v; do tr["$k"]="$v"; done < "$trfile"; fi

  if [[ -z "$url" ]]; then
    echo "${tr[missing_url]:-❌ [download] Falta el parámetro obligatorio 'url'}"
    return 1
  fi

  local cmd=""
  if command -v wget &>/dev/null; then
    echo "${tr[using_wget]:-📦 [download] Usando wget}"
    cmd="$prefix wget \"$url\" -O \"$dest\""
    [[ "$continue" == "true" ]] && cmd="$cmd -c"
    [[ -n "$proxy" ]] && cmd="$cmd -e use_proxy=yes -e http_proxy=\"$proxy\""
  elif command -v curl &>/dev/null; then
    echo "${tr[using_curl]:-📦 [download] Usando curl}"
    cmd="$prefix curl -L \"$url\" -o \"$dest\""
    [[ "$continue" == "true" ]] && cmd="$cmd -C -"
    [[ -n "$proxy" ]] && cmd="$cmd --proxy \"$proxy\""
  else
    echo "${tr[missing_tool]:-❌ [download] Ni wget ni curl están disponibles}"
    return 1
  fi

  echo "$(render_msg "${tr[start]}" "url=$url" "dest=$dest")"
  eval "$cmd" && echo "$(render_msg "${tr[done]}" "dest=$dest")"
}

check_dependencies_download() {
  local lang="${shflow_vars[language]:-es}"
  local trfile="$(dirname "${BASH_SOURCE[0]}")/download.tr.${lang}"
  declare -A tr
  if [[ -f "$trfile" ]]; then while IFS='=' read -r k v; do tr["$k"]="$v"; done < "$trfile"; fi

  if ! command -v wget &>/dev/null && ! command -v curl &>/dev/null; then
    echo "${tr[missing_tool]:-❌ [download] Se requiere 'wget' o 'curl'}"
    return 1
  fi
  echo "${tr[deps_ok]:-✅ [download] Herramienta de descarga disponible}"
  return 0
}