@איל-משולש כתב:
שימו לב! הקבצים יעלו עם השם המדוייק שיש להם בדרייב, ולא עם מספר סידורי!
אפשר להוסיף בפונקציה processUpload הגדרה של מונה כדי שהקבצים יעלו ממוספרים (000, 001 וכו'), הנה הקוד המלא עם המונה:
// ==========================================
// הגדרות קבועות בקוד
// ==========================================
const FOLDER_ID = 'הכנס_כאן_את_מזהה_התיקייה_שלך';
// אם המשתנה מוגדר (למשל '1/2') - ישתמש בו תמיד ולא יבקש בממשק.
// אם נשאיר אותו ריק ('') - ישתמש בפרמטר הנתיב מ-URL או יבקש אותו בממשק.
const DEFAULT_PATH = '';
function doGet(e) {
var params = e ? e.parameter : {};
var token = params.token;
// קביעת הנתיב בפועל:
// אם DEFAULT_PATH לא ריק -> משתמשים בו תמיד.
// אם הוא ריק -> משתמשים בפרמטר path שנשלח ב-URL.
var effectivePath = (DEFAULT_PATH && DEFAULT_PATH.trim() !== '') ? DEFAULT_PATH : params.path;
// אם חסר טוקן, או שחסר נתיב (במקרה ש-DEFAULT_PATH ריק ולא נשלח path ב-URL) -> מציגים UI
if (!token || !effectivePath) {
return createUI(effectivePath);
}
// ביצוע ההעלאה
return processUpload(token, effectivePath);
}
/**
* יצירת ממשק המשתמש (דינמי לפי מצב ה-DEFAULT_PATH)
*/
function createUI(currentPath) {
// בדיקה האם יש נתיב מוגדר מראש
var hasFixedPath = DEFAULT_PATH && DEFAULT_PATH.trim() !== '';
var htmlContent = `
<!DOCTYPE html>
<html dir="rtl" lang="he">
<head>
<meta charset="UTF-8">
<title>העלאת קבצים לימות המשיח</title>
<style>
body { font-family: Arial, sans-serif; background-color: #f4f6f9; margin: 0; padding: 40px; display: flex; justify-content: center; }
.card { background: white; padding: 30px; border-radius: 8px; box-shadow: 0 4px 10px rgba(0,0,0,0.1); width: 100%; max-width: 400px; }
h2 { text-align: center; color: #333; margin-top: 0; }
.info-path { background-color: #e9ecef; padding: 10px; border-radius: 4px; font-size: 14px; color: #495057; margin-bottom: 20px; text-align: center; }
.form-group { margin-bottom: 15px; }
label { display: block; margin-bottom: 5px; font-weight: bold; color: #555; }
input[type="text"] { width: 100%; padding: 10px; border: 1px solid #ccc; border-radius: 4px; box-sizing: border-box; }
button { width: 100%; padding: 12px; background-color: #007bff; color: white; border: none; border-radius: 4px; font-size: 16px; cursor: pointer; }
button:hover { background-color: #0056b3; }
#status { margin-top: 15px; font-weight: bold; text-align: center; }
</style>
</head>
<body>
<div class="card">
<h2>העלאת קבצים לימות</h2>
${hasFixedPath ? `
<div class="info-path">
הקבצים יעלו אוטומטית לנתיב: <strong>ivr2:${DEFAULT_PATH}</strong>
</div>
` : ''}
<form id="uploadForm">
<div class="form-group">
<label for="token">טוקן (Token):</label>
<input type="text" id="token" name="token" required placeholder="הכנס טוקן...">
</div>
${!hasFixedPath ? `
<div class="form-group">
<label for="path">נתיב (Path):</label>
<input type="text" id="path" name="path" required placeholder="לדוגמה: 1/2">
</div>
` : ''}
<button type="submit">התחל העלאה</button>
</form>
<div id="status"></div>
</div>
<script>
var hasFixedPath = ${hasFixedPath};
document.getElementById('uploadForm').addEventListener('submit', function(e) {
e.preventDefault();
var token = document.getElementById('token').value.trim();
var statusDiv = document.getElementById('status');
statusDiv.style.color = '#333';
statusDiv.innerText = 'מעלה קבצים, אנא המתן...';
var targetUrl = window.location.href.split('?')[0] + '?token=' + encodeURIComponent(token);
// אם אין נתיב קבוע, מוסיפים את הנתיב מהשדה בטופס
if (!hasFixedPath) {
var inputPath = document.getElementById('path').value.trim();
targetUrl += '&path=' + encodeURIComponent(inputPath);
}
fetch(targetUrl)
.then(response => response.text())
.then(data => {
if (data.includes('OK:')) {
statusDiv.style.color = 'green';
} else {
statusDiv.style.color = 'red';
}
statusDiv.innerText = data;
})
.catch(err => {
statusDiv.style.color = 'red';
statusDiv.innerText = 'שגיאה בחיבור: ' + err;
});
});
</script>
</body>
</html>
`;
return HtmlService.createHtmlOutput(htmlContent)
.setTitle('העלאת קבצים לימות המשיח')
.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
}
/**
* פונקציית העלאת הקבצים
*/
function processUpload(token, ivrPath) {
if (ivrPath.endsWith('/')) {
ivrPath = ivrPath.slice(0, -1);
}
if (!ivrPath.startsWith('ivr2:')) {
ivrPath = 'ivr2:' + ivrPath;
}
try {
var folder = DriveApp.getFolderById(FOLDER_ID);
var files = folder.getFiles();
var uploadedCount = 0;
var errorsCount = 0;
// מונה למספור הקבצים (0, 1, 2...)
var counter = 0;
while (files.hasNext()) {
var file = files.next();
var mimeType = file.getMimeType();
var fileName = file.getName().toLowerCase();
var isAudio = mimeType.startsWith('audio/') ||
fileName.endsWith('.wav') ||
fileName.endsWith('.mp3') ||
fileName.endsWith('.ogg') ||
fileName.endsWith('.wma');
if (!isAudio) {
continue;
}
// יצירת שם קובץ חדש במספור סידורי עם סיומת .wav
var newFileName = String(counter).padStart(3, '0') + '.wav';
var fileBlob = file.getBlob();
var fullPath = ivrPath + '/' + newFileName;
var yemotUrl = 'https://www.call2all.co.il/ym/api/UploadFile' +
'?token=' + encodeURIComponent(token) +
'&path=' + encodeURIComponent(fullPath) +
'&convertAudio=1';
var options = {
'method': 'post',
'payload': {
'file': fileBlob
},
'muteHttpExceptions': true
};
var response = UrlFetchApp.fetch(yemotUrl, options);
var responseText = response.getContentText();
if (responseText.includes('OK')) {
uploadedCount++;
counter++; // קידום המונה לקובץ הבא
} else {
errorsCount++;
}
}
return ContentService.createTextOutput("OK: Uploaded " + uploadedCount + " files. Errors: " + errorsCount);
} catch (error) {
return ContentService.createTextOutput("EXCEPTION: " + error.toString());
}
}