Changed so that root folder is not a DB requirement and not created on the fly.

This commit is contained in:
Ziver Koc 2018-08-13 21:10:08 +02:00
parent 5921a08b14
commit 8ea4f0d6d5
4 changed files with 91 additions and 85 deletions

View file

@ -7,7 +7,6 @@
--
-- Table structure for table `Comments`
--
CREATE TABLE `Comments` (
`id` int(11) NOT NULL,
`image` int(11) DEFAULT NULL,
@ -17,12 +16,24 @@ CREATE TABLE `Comments` (
`message` text
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
--
-- Indexes for table `Comments`
--
ALTER TABLE `Comments`
ADD PRIMARY KEY (`id`),
ADD KEY `image` (`image`,`video`);
--
-- AUTO_INCREMENT for table `Comments`
--
ALTER TABLE `Comments`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=18;
-- --------------------------------------------------------
--
-- Table structure for table `Folder`
--
CREATE TABLE `Folder` (
`id` int(10) UNSIGNED NOT NULL,
`owner` int(10) UNSIGNED DEFAULT NULL,
@ -32,12 +43,29 @@ CREATE TABLE `Folder` (
`isPrivate` bit(1) DEFAULT b'0'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Indexes for table `Folder`
--
ALTER TABLE `Folder`
ADD PRIMARY KEY (`id`);
--
-- AUTO_INCREMENT for table `Folder`
--
ALTER TABLE `Folder`
MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=46;
--
-- Data for table `Folder`
--
INSERT INTO `Folder` (`owner`, `name`, `parent`, `dateCreated`, `isPrivate`) VALUES
(0, '/', NULL, '2011-01-24 21:35:34', b'0');
-- --------------------------------------------------------
--
-- Table structure for table `Image`
--
CREATE TABLE `Image` (
`id` int(10) UNSIGNED NOT NULL,
`folder` int(10) UNSIGNED NOT NULL,
@ -49,12 +77,24 @@ CREATE TABLE `Image` (
`dateUploaded` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Indexes for table `Image`
--
ALTER TABLE `Image`
ADD PRIMARY KEY (`id`) USING BTREE,
ADD KEY `folder` (`folder`);
--
-- AUTO_INCREMENT for table `Image`
--
ALTER TABLE `Image`
MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1713;
-- --------------------------------------------------------
--
-- Table structure for table `User`
--
CREATE TABLE `User` (
`id` int(11) NOT NULL,
`name` varchar(40) NOT NULL,
@ -71,18 +111,30 @@ CREATE TABLE `User` (
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `User`
-- Indexes for table `User`
--
ALTER TABLE `User`
ADD PRIMARY KEY (`id`),
ADD KEY `email` (`email`),
ADD KEY `cookieHash` (`cookieHash`);
INSERT INTO `User` (`id`, `name`, `email`, `emailVerified`, `passwordHash`, `passwordSalt`, `ipHost`, `dateCreated`, `dateLastLogin`, `cookieHash`, `superUser`, `enabled`) VALUES
(1, 'Admin Admin', 'admin', 1, '6e88be8bad7eae9d9e10aa061224034fed48d03fcbad968b56006784539d5214', 'salt', '', '1970-01-01 01:00:0', '1970-01-01 01:00:0', '', 1, 1);
--
-- AUTO_INCREMENT for table `User`
--
ALTER TABLE `User`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=44;
--
-- Data for table `User`
--
INSERT INTO `User` (`name`, `email`, `emailVerified`, `passwordHash`, `passwordSalt`, `ipHost`, `dateCreated`, `dateLastLogin`, `cookieHash`, `superUser`, `enabled`) VALUES
('Admin Admin', 'admin', 1, '6e88be8bad7eae9d9e10aa061224034fed48d03fcbad968b56006784539d5214', 'salt', '', '1970-01-01 01:00:0', '1970-01-01 01:00:0', '', 1, 1);
-- --------------------------------------------------------
--
-- Table structure for table `Video`
--
CREATE TABLE `Video` (
`id` int(10) UNSIGNED NOT NULL,
`folder` int(10) UNSIGNED NOT NULL,
@ -96,34 +148,6 @@ CREATE TABLE `Video` (
`length` int(10) UNSIGNED NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Indexes for table `Comments`
--
ALTER TABLE `Comments`
ADD PRIMARY KEY (`id`),
ADD KEY `image` (`image`,`video`);
--
-- Indexes for table `Folder`
--
ALTER TABLE `Folder`
ADD PRIMARY KEY (`id`);
--
-- Indexes for table `Image`
--
ALTER TABLE `Image`
ADD PRIMARY KEY (`id`) USING BTREE,
ADD KEY `folder` (`folder`);
--
-- Indexes for table `User`
--
ALTER TABLE `User`
ADD PRIMARY KEY (`id`),
ADD KEY `email` (`email`),
ADD KEY `cookieHash` (`cookieHash`);
--
-- Indexes for table `Video`
--
@ -132,31 +156,9 @@ ALTER TABLE `Video`
ADD KEY `folder` (`folder`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `Comments`
--
ALTER TABLE `Comments`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=18;
--
-- AUTO_INCREMENT for table `Folder`
--
ALTER TABLE `Folder`
MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=46;
--
-- AUTO_INCREMENT for table `Image`
--
ALTER TABLE `Image`
MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1713;
--
-- AUTO_INCREMENT for table `User`
--
ALTER TABLE `User`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=44;
--
-- AUTO_INCREMENT for table `Video`
--
ALTER TABLE `Video`
MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT;
-- --------------------------------------------------------

View file

@ -35,7 +35,6 @@ INSERT INTO `Comments` (`id`, `image`, `video`, `user`, `dateCreated`, `message`
--
INSERT INTO `Folder` (`id`, `owner`, `name`, `parent`, `dateCreated`, `isPrivate`) VALUES
(1, 0, '/', NULL, '2011-01-24 21:35:34', b'0'),
(11, 20, '{NAME}', 1, '2011-02-15 21:24:44', b'1'),
(12, 20, 'test', 11, '2011-02-15 21:24:44', b'0'),
(13, 6, '{NAME}', 1, '2011-03-19 23:50:30', b'0'),
@ -1655,3 +1654,24 @@ INSERT INTO `User` (`id`, `name`, `email`, `emailVerified`, `passwordHash`, `pas
(43, 'User 13', 'test@example.com', 0, '6e88be8bad7eae9d9e10aa061224034fed48d03fcbad968b56006784539d5214', 'salt', '91.7.239.138', '2017-08-01 23:48:15', '20a8626ffec27e2f90dc4bbaace4f995', 0, 1);
-- --------------------------------------------------------
--
-- AUTO_INCREMENT for table `Comments`
--
ALTER TABLE `Comments`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=18;
--
-- AUTO_INCREMENT for table `Folder`
--
ALTER TABLE `Folder`
MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=46;
--
-- AUTO_INCREMENT for table `Image`
--
ALTER TABLE `Image`
MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1713;
--
-- AUTO_INCREMENT for table `User`
--
ALTER TABLE `User`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=44;

View file

@ -28,19 +28,14 @@ public class Folder extends DBBean {
return load(db, Folder.class, id);
}
public static List<Folder> loadSubFolders(DBConnection db, Folder folder, User requestingUser) throws SQLException {
PreparedStatement sql = db.getPreparedStatement("SELECT * FROM Folder WHERE parent=? AND (isPrivate=0 OR owner=? OR ?)");
public static List<Folder> loadSubFolders(DBConnection db, Folder folder) throws SQLException {
PreparedStatement sql = db.getPreparedStatement("SELECT * FROM Folder WHERE parent=?");
sql.setLong(1, folder.getId());
sql.setLong(2, folder.owner.getId());
sql.setBoolean(3, requestingUser.isSuperUser());
return DBConnection.exec(sql, DBBeanSQLResultHandler.createList(Folder.class, db));
}
public static Folder loadRoot(DBConnection db, User user) throws SQLException {
PreparedStatement sql = db.getPreparedStatement("SELECT * FROM Folder WHERE name=? AND (isPrivate=0 OR owner=? OR ?)");
sql.setString(1, "/");
sql.setLong(2, user.getId());
sql.setBoolean(3, user.isSuperUser());
PreparedStatement sql = db.getPreparedStatement("SELECT * FROM Folder WHERE name='/' AND parent IS NULL LIMIT 1");
return DBConnection.exec(sql, DBBeanSQLResultHandler.create(Folder.class, db));
}
@ -93,16 +88,8 @@ public class Folder extends DBBean {
return dateCreated;
}
public static Folder createRootFolder() {
Folder root = new Folder();
root.parent = null;
root.owner = null;
root.name = "/";
return root;
}
public boolean isEmpty(DBConnection db) throws SQLException {
return Folder.loadSubFolders(db, this, getOwner()).isEmpty() && Media.load(db, this).isEmpty();
return Folder.loadSubFolders(db, this).isEmpty() &&
Media.load(db, this).isEmpty();
}
}

View file

@ -38,15 +38,12 @@ public class GalleryServlet extends ZalleryServlet {
folder = Folder.load(db, Long.parseLong(request.getParameter("folder")));
else {
folder = Folder.loadRoot(db, user);
// Setup new root folder
if (folder == null) {
folder = Folder.createRootFolder();
folder.save(db);
}
if (folder == null)
throw new NullPointerException("No root folder found in DB.");
}
List<Media> list = Media.load(db, folder);
List<Folder> subFolders = Folder.loadSubFolders(db, folder, user);
List<Folder> subFolders = Folder.loadSubFolders(db, folder);
//session.setAttribute("user", user);
request.setAttribute("folder", folder);