view mrjunejune/src/tools/file_converter/index.js @ 160:948de3f54cea

[ThirdParty] Added libuv
author June Park <parkjune1995@gmail.com>
date Wed, 14 Jan 2026 19:39:52 -0800
parents be91a73d801a
children
line wrap: on
line source

async function convertImageToWebP()
{
  const input = document.getElementById('imageInput');
  const btn = document.getElementById('convertImageBtn');
  const loading = document.getElementById('imageLoading');
  const result = document.getElementById('imageResult');
  const message = document.getElementById('imageMessage');
  const download = document.getElementById('imageDownload');

  if (!input.files || !input.files[0])
  {
    alert('Please select an image file first');
    return;
  }

  const file = input.files[0];

  // Show loading state
  btn.disabled = true;
  loading.classList.add('show');
  result.classList.remove('show', 'success', 'error');
  download.style.display = 'none';

  try {
    const response = await fetch('/api/convert/image-to-webp', {
      method: 'POST',
      body: file,
      headers: {
        'Content-Type': file.type
      }
    });

    loading.classList.remove('show');
    result.classList.add('show');

    if (response.ok) {
      const data = await response.json();

      download.href = data.download_url;
      download.download = file.name.replace(/\.[^/.]+$/, '') + '.webp';
      download.style.display = 'inline-block';

      message.textContent = 'Conversion successful! Link expires in ' + data.expires + '.';
      result.classList.add('success');
    } else {
      const text = await response.text();
      message.textContent = 'Conversion failed: ' + text;
      result.classList.add('error');
    }
  } catch (error) {
    loading.classList.remove('show');
    result.classList.add('show', 'error');
    message.textContent = 'Error: ' + error.message;
  } finally {
    btn.disabled = false;
  }
}

async function convertVideoToMP4() {
  const input = document.getElementById('videoInput');
  const btn = document.getElementById('convertVideoBtn');
  const loading = document.getElementById('videoLoading');
  const result = document.getElementById('videoResult');
  const message = document.getElementById('videoMessage');
  const download = document.getElementById('videoDownload');

  if (!input.files || !input.files[0]) {
    alert('Please select a video file first');
    return;
  }

  const file = input.files[0];

  // Show loading state
  btn.disabled = true;
  loading.classList.add('show');
  result.classList.remove('show', 'success', 'error');
  download.style.display = 'none';

  try {
    const response = await fetch('/api/convert/video-to-mp4', {
      method: 'POST',
      body: file,
      headers: {
        'Content-Type': file.type
      }
    });

    loading.classList.remove('show');
    result.classList.add('show');

    if (response.ok) {
      const data = await response.json();

      download.href = data.download_url;
      download.download = file.name.replace(/\.[^/.]+$/, '') + '.mp4';
      download.style.display = 'inline-block';

      message.textContent = 'Conversion successful! Link expires in ' + data.expires + '.';
      result.classList.add('success');
    } else {
      const text = await response.text();
      message.textContent = 'Conversion failed: ' + text;
      result.classList.add('error');
    }
  } catch (error) {
    loading.classList.remove('show');
    result.classList.add('show', 'error');
    message.textContent = 'Error: ' + error.message;
  } finally {
    btn.disabled = false;
  }
}