File size: 4,646 Bytes
fe8f8e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// מזהה התיקייה הראשית בדרייב שבה יישבו כל הכיתות
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);
  }
}