yonatansheiner
Update Google Script URL
fe8f8e5
Raw History Blame Contribute Delete
4.65 kB
// מזהה התיקייה הראשית בדרייב שבה יישבו כל הכיתות
const ROOT_FOLDER_ID = '1ZmvMINBH1ugWSDgD88Otb2jOM8-ZLsVt';
// עוזר: מציאת או יצירת תיקיית כיתה בתוך התיקייה הראשית
function getOrCreateClassFolder(rootFolder, className) {
if (!className) {
className = 'Unknown';
}
const folders = rootFolder.getFoldersByName(className);
if (folders.hasNext()) {
return folders.next();
}
return rootFolder.createFolder(className);
}
// עוזר: מציאת או יצירת תיקיית social_media בתוך התיקייה הראשית
function getOrCreateSocialMediaFolder(rootFolder) {
const folders = rootFolder.getFoldersByName('social_media');
if (folders.hasNext()) {
return folders.next();
}
return rootFolder.createFolder('social_media');
}
// עוזר: אופציונלי – ליצור תת־תיקייה לפי תאריך בתוך תיקיית הכיתה
function getOrCreateDateFolder(classFolder) {
const today = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), 'yyyy-MM-dd');
const folders = classFolder.getFoldersByName(today);
if (folders.hasNext()) {
return folders.next();
}
return classFolder.createFolder(today);
}
// עוזר: שמירת קובץ בתיקייה
function saveFileToFolder(folder, blob, finalName, descriptionParts) {
const file = folder.createFile(blob);
if (descriptionParts && descriptionParts.length > 0) {
file.setDescription(descriptionParts.join(' | '));
}
return file;
}
// נקודת הכניסה ל־Web App – מקבלת POST מהשרת שלך
function doPost(e) {
try {
// קריאת הגוף שנשלח מהשרת (JSON)
const data = JSON.parse(e.postData.contents);
// מצפה לשדות:
// photo: base64 (בלי data:image/...)
// mimeType: 'image/jpeg'
// filename: שם קובץ בסיסי
// caption: טקסט (לא חובה)
// className: שם כיתה (למשל 'א1')
// fullName: שם תלמיד (לא חובה)
// shareOnSocial: true/false – האם לאשר העלאה לרשתות חברתיות
const rootFolder = DriveApp.getFolderById(ROOT_FOLDER_ID);
// תיקיית כיתה
const classFolder = getOrCreateClassFolder(rootFolder, data.className);
// אופציונלי: תת־תיקייה לפי תאריך
const dateFolder = getOrCreateDateFolder(classFolder);
const contentType = data.mimeType || 'image/jpeg';
// שם קובץ: תאריך־שעה + שם (אם יש)
const timestamp = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), 'yyyyMMdd-HHmmss');
const safeNamePart = (data.fullName || '').trim().replace(/[^\u0590-\u05FF\w\s-]/g, '');
const baseFileName = data.filename || 'photo';
const finalName =
(safeNamePart
? timestamp + '-' + safeNamePart + '.jpg'
: timestamp + '-' + baseFileName + '.jpg');
// המרת Base64 לבייטים
const base64Data = data.photo;
const bytes = Utilities.base64Decode(base64Data);
const blob = Utilities.newBlob(bytes, contentType, finalName);
// description – אפשר לשמור כיתוב ושם
const descriptionParts = [];
if (data.caption) descriptionParts.push('caption: ' + data.caption);
if (data.fullName) descriptionParts.push('name: ' + data.fullName);
if (data.className) descriptionParts.push('class: ' + data.className);
// 1. שמירה רגילה בתיקיית הכיתה/תאריך
const file = saveFileToFolder(dateFolder, blob, finalName, descriptionParts);
// 2. אם אושר שיתוף ברשתות – שמירה נוספת בתיקיית social_media
let socialFileId = null;
if (data.shareOnSocial === true || data.shareOnSocial === 'true') {
const socialFolder = getOrCreateSocialMediaFolder(rootFolder);
const socialDateFolder = getOrCreateDateFolder(socialFolder);
const socialBlob = Utilities.newBlob(bytes, contentType, finalName);
const socialFile = saveFileToFolder(socialDateFolder, socialBlob, finalName, descriptionParts);
socialFileId = socialFile.getId();
}
return ContentService
.createTextOutput(JSON.stringify({
status: 'ok',
fileId: file.getId(),
classFolder: classFolder.getName(),
socialFileId: socialFileId
}))
.setMimeType(ContentService.MimeType.JSON);
} catch (err) {
return ContentService
.createTextOutput(JSON.stringify({
status: 'error',
message: err.toString()
}))
.setMimeType(ContentService.MimeType.JSON);
}
}