Spaces:
Sleeping
Sleeping
| const mongoose = require('mongoose'); | |
| // Atlas MongoDB connection string | |
| const MONGODB_URI = 'mongodb+srv://nothingyu:wSg3lbO1PkHiRMq9@sandbox.ecysggv.mongodb.net/test?retryWrites=true&w=majority&appName=sandbox'; | |
| // Connect to MongoDB Atlas | |
| const connectDB = async () => { | |
| try { | |
| await mongoose.connect(MONGODB_URI); | |
| console.log('β Connected to MongoDB Atlas'); | |
| } catch (error) { | |
| console.error('β MongoDB connection error:', error); | |
| process.exit(1); | |
| } | |
| }; | |
| // Subtitle Schema (same as in models/Subtitle.js) | |
| const subtitleSchema = new mongoose.Schema({ | |
| segmentId: { type: Number, required: true, unique: true }, | |
| startTime: { type: String, required: true }, | |
| endTime: { type: String, required: true }, | |
| duration: { type: String, required: true }, | |
| englishText: { type: String, required: true }, | |
| chineseTranslation: { type: String, default: '' }, | |
| isProtected: { type: Boolean, default: false }, | |
| protectedReason: { type: String, default: '' }, | |
| lastModified: { type: Date, default: Date.now }, | |
| modificationHistory: [{ | |
| timestamp: { type: Date, default: Date.now }, | |
| action: String, | |
| previousValue: String, | |
| newValue: String, | |
| modifiedBy: String | |
| }] | |
| }); | |
| const Subtitle = mongoose.model('Subtitle', subtitleSchema); | |
| const lockSubtitles = async () => { | |
| try { | |
| console.log('π Starting subtitle protection process...'); | |
| // Find all subtitle segments | |
| const subtitles = await Subtitle.find({}); | |
| console.log(`π Found ${subtitles.length} subtitle segments`); | |
| if (subtitles.length === 0) { | |
| console.log('β No subtitle segments found. Please seed the database first.'); | |
| return; | |
| } | |
| // Lock all subtitle segments | |
| const updatePromises = subtitles.map(subtitle => | |
| Subtitle.findByIdAndUpdate( | |
| subtitle._id, | |
| { | |
| isProtected: true, | |
| protectedReason: 'Original English subtitles and timecodes - critical for course content', | |
| lastModified: new Date(), | |
| $push: { | |
| modificationHistory: { | |
| timestamp: new Date(), | |
| action: 'PROTECTED', | |
| previousValue: 'false', | |
| newValue: 'true', | |
| modifiedBy: 'system' | |
| } | |
| } | |
| }, | |
| { new: true } | |
| ) | |
| ); | |
| const updatedSubtitles = await Promise.all(updatePromises); | |
| console.log(`β Successfully protected ${updatedSubtitles.length} subtitle segments`); | |
| // Verify protection | |
| const protectedCount = await Subtitle.countDocuments({ isProtected: true }); | |
| console.log(`π Verification: ${protectedCount} segments are now protected`); | |
| // Show sample of protected segments | |
| const sampleProtected = await Subtitle.find({ isProtected: true }).limit(5); | |
| console.log('\nπ Sample of protected segments:'); | |
| sampleProtected.forEach(subtitle => { | |
| console.log(` Segment ${subtitle.segmentId}: "${subtitle.englishText.substring(0, 50)}..."`); | |
| }); | |
| console.log('\nπ― Subtitle protection complete!'); | |
| console.log('π Protection details:'); | |
| console.log(' - All 26 subtitle segments are now protected'); | |
| console.log(' - Original English text cannot be modified'); | |
| console.log(' - Timecodes cannot be changed'); | |
| console.log(' - Chinese translations can still be added/updated'); | |
| console.log(' - Use unlock-subtitles.js to temporarily disable protection'); | |
| } catch (error) { | |
| console.error('β Error protecting subtitles:', error); | |
| } finally { | |
| await mongoose.disconnect(); | |
| console.log('π Disconnected from MongoDB'); | |
| } | |
| }; | |
| // Run the protection script | |
| connectDB().then(() => { | |
| lockSubtitles(); | |
| }); |