File size: 4,713 Bytes
364934b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ce9b06
364934b
 
 
6ce9b06
364934b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
class CustomFileUpload extends HTMLElement {
  connectedCallback() {
    this.attachShadow({ mode: 'open' });
    this.shadowRoot.innerHTML = `
      <style>
        :host {
          display: block;
          position: fixed;
          bottom: 80px;
          left: 280px;
          right: 40%;
          background: white;
          padding: 1rem;
          border: 1px solid #e2e8f0;
          border-radius: 0.5rem;
          box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
          z-index: 10;
        }
        .drop-area {
          border: 2px dashed #cbd5e1;
          border-radius: 0.5rem;
          padding: 2rem;
          text-align: center;
          cursor: pointer;
        }
        .drop-area.highlight {
          border-color: #3b82f6;
          background-color: #f0f9ff;
        }
        .file-list {
          margin-top: 1rem;
          max-height: 200px;
          overflow-y: auto;
        }
        .file-item {
          display: flex;
          align-items: center;
          padding: 0.5rem;
          border-bottom: 1px solid #e2e8f0;
        }
        .file-icon {
          margin-right: 0.5rem;
        }
        .upload-button {
          margin-top: 1rem;
          background: #3b82f6;
          color: white;
          border: none;
          border-radius: 0.5rem;
          padding: 0.5rem 1rem;
          cursor: pointer;
        }
        .upload-button:hover {
          background: #2563eb;
        }
      </style>
      <div class="drop-area" id="dropArea">
        <i data-feather="upload-cloud"></i>
        <p>Glissez-déposez vos fichiers ici ou cliquez pour sélectionner</p>
        <input type="file" id="fileInput" multiple style="display: none;">
        <div class="file-list" id="fileList"></div>
        <button class="upload-button" id="uploadButton">Envoyer vers Rosalinda</button>
      </div>
    `;

    const dropArea = this.shadowRoot.getElementById('dropArea');
    const fileInput = this.shadowRoot.getElementById('fileInput');
    const fileList = this.shadowRoot.getElementById('fileList');
    const uploadButton = this.shadowRoot.getElementById('uploadButton');

    let files = [];

    dropArea.addEventListener('click', () => fileInput.click());
    
    fileInput.addEventListener('change', (e) => {
      files = Array.from(e.target.files);
      updateFileList();
    });

    ['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
      dropArea.addEventListener(eventName, preventDefaults, false);
    });

    ['dragenter', 'dragover'].forEach(eventName => {
      dropArea.addEventListener(eventName, highlight, false);
    });

    ['dragleave', 'drop'].forEach(eventName => {
      dropArea.addEventListener(eventName, unhighlight, false);
    });

    dropArea.addEventListener('drop', handleDrop, false);

    uploadButton.addEventListener('click', uploadFiles);

    function preventDefaults(e) {
      e.preventDefault();
      e.stopPropagation();
    }

    function highlight() {
      dropArea.classList.add('highlight');
    }

    function unhighlight() {
      dropArea.classList.remove('highlight');
    }

    function handleDrop(e) {
      const dt = e.dataTransfer;
      files = Array.from(dt.files);
      updateFileList();
    }

    function updateFileList() {
      fileList.innerHTML = '';
      files.forEach(file => {
        const fileItem = document.createElement('div');
        fileItem.className = 'file-item';
        fileItem.innerHTML = `
          <i data-feather="file" class="file-icon"></i>
          <span>${file.name} (${formatFileSize(file.size)})</span>
        `;
        fileList.appendChild(fileItem);
      });
      feather.replace();
    }

    async function uploadFiles() {
      if (files.length === 0) return;

      const formData = new FormData();
      files.forEach(file => formData.append('files', file));

      try {
        const response = await fetch('/api/files/upload', {
          method: 'POST',
          body: formData
        });
const data = await response.json();
        if (data.ok) {
          alert(`${data.files.length} fichiers envoyés avec succès à Rosalinda`);
          files = [];
          updateFileList();
        }
      } catch (error) {
        console.error('Error uploading files:', error);
        alert('Erreur lors de l\'envoi des fichiers');
      }
    }

    function formatFileSize(bytes) {
      if (bytes === 0) return '0 Bytes';
      const k = 1024;
      const sizes = ['Bytes', 'KB', 'MB', 'GB'];
      const i = Math.floor(Math.log(bytes) / Math.log(k));
      return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
    }
  }
}

customElements.define('custom-file-upload', CustomFileUpload);