All files / Backend/Services driveService.js

34.14% Statements 14/41
0% Branches 0/6
0% Functions 0/5
34.14% Lines 14/41

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                        1x 1x 1x 1x     1x 1x     1x 1x                                     1x         1x     1x                                                                                 1x                         1x                                                                             1x            
/*
 * Google Drive Folder Structure (Planned/Current):
 *
 * 1. Event Images:
 *    - Logos are uploaded to the folder with ID process.env.DRIVE_LOGO_FOLDER_ID
 *    - Banners are uploaded to the folder with ID process.env.DRIVE_BANNER_FOLDER_ID
 *    - This ensures separation of logo and banner images for all events.
 *
 * 2. Certificates (Planned):
 *    - For each event, a new folder will be created in Drive, named after the event (e.g., "EventName_Certificates").
 *    - All certificates generated for that event (by ML API) will be stored in this folder.
 *    - Each certificate file will be linked to the corresponding user in the database, so users can access/download their certificates.
 *    - (Future) Optionally, users may be able to browse/download their certificates directly from the event-named folder in Drive.
 *
 * 3. ML Integration:
 *    - Certificate generation and event recommendations will be handled by external ML APIs.
 *    - Backend endpoints will call these ML APIs and store results (certificates, recommendations) as needed.
 *
 * These plans are for reference/documentation and do not affect current code execution.
 */
const { google } = require('googleapis');
const multer = require('multer');
const stream = require('stream');
const path = require('path');
 
// Google Drive API configuration
const SCOPES = ['https://www.googleapis.com/auth/drive.file'];
const FOLDER_ID = process.env.GOOGLE_DRIVE_FOLDER_ID; // Create a folder in Drive and get its ID
 
// Configure multer for file upload
const storage = multer.memoryStorage();
const upload = multer({
  storage,
  limits: {
    fileSize: 5 * 1024 * 1024, // 5MB limit
  },
  fileFilter: (req, file, cb) => {
    const allowedTypes = /jpeg|jpg|png|gif/;
    const extname = allowedTypes.test(path.extname(file.originalname).toLowerCase());
    const mimetype = allowedTypes.test(file.mimetype);
    
    if (mimetype && extname) {
      return cb(null, true);
    } else {
      cb(new Error('Only image files are allowed!'));
    }
  }
});
 
// Initialize Google Drive API
const auth = new google.auth.GoogleAuth({
  keyFile: process.env.GOOGLE_DRIVE_KEY_FILE, // Path to your service account key file
  scopes: SCOPES,
});
 
const drive = google.drive({ version: 'v3', auth });
 
// Upload image to Google Drive
const uploadImageToDrive = async (file, userId) => {
  try {
    const fileName = `profile_${userId}_${Date.now()}${path.extname(file.originalname)}`;
    
    const fileMetadata = {
      name: fileName,
      parents: [FOLDER_ID],
    };
 
    const media = {
      mimeType: file.mimetype,
      body: file.buffer,
    };
 
    const response = await drive.files.create({
      resource: fileMetadata,
      media,
      fields: 'id, webViewLink, webContentLink',
    });
 
    // Make the file publicly accessible
    await drive.permissions.create({
      fileId: response.data.id,
      requestBody: {
        role: 'reader',
        type: 'anyone',
      },
    });
 
    return {
      fileId: response.data.id,
      url: `https://drive.google.com/uc?export=view&id=${response.data.id}`,
      fileName,
    };
  } catch (error) {
    console.error('Error uploading to Google Drive:', error);
    throw new Error('Failed to upload image');
  }
};
 
// Delete image from Google Drive
const deleteImageFromDrive = async (fileId) => {
  try {
    await drive.files.delete({
      fileId,
    });
    return true;
  } catch (error) {
    console.error('Error deleting from Google Drive:', error);
    throw new Error('Failed to delete image');
  }
};
 
// Get image URL from Google Drive
const getImageUrl = (fileId) => {
  return `https://drive.google.com/uc?export=view&id=${fileId}`;
};
 
/**
 * Uploads an event image (logo or banner) to Google Drive and returns the file URL.
 * @param {Buffer} fileBuffer - The file buffer to upload
 * @param {String} filename - The name of the file
 * @param {String} type - 'logo' or 'banner'
 * @returns {Promise<String>} - The public URL of the uploaded file
 */
async function uploadEventImage(fileBuffer, filename, type) {
  const folderId = type === 'logo'
    ? process.env.DRIVE_LOGO_FOLDER_ID
    : process.env.DRIVE_BANNER_FOLDER_ID;
  const bufferStream = new stream.PassThrough();
  bufferStream.end(fileBuffer);
 
  const { data } = await drive.files.create({
    requestBody: {
      name: filename,
      parents: [folderId],
      mimeType: 'image/png', // or detect from file
    },
    media: {
      mimeType: 'image/png',
      body: bufferStream,
    },
    fields: 'id',
  });
 
  await drive.permissions.create({
    fileId: data.id,
    requestBody: { role: 'reader', type: 'anyone' },
  });
 
  return `https://drive.google.com/uc?export=view&id=${data.id}`;
}
 
module.exports = {
  upload,
  uploadImageToDrive,
  deleteImageFromDrive,
  getImageUrl,
  uploadEventImage
};